import { TAB, NamingConvention, getName } from "project-editor/build/helper"; import type { Style } from "project-editor/features/style/style"; import type { Assets, DataBuffer } from "project-editor/build/assets"; import { Section } from "project-editor/store"; import { MessageType } from "project-editor/core/object"; export const STYLE_FLAGS_HORZ_ALIGN_LEFT = 0; export const STYLE_FLAGS_HORZ_ALIGN_RIGHT = 1; export const STYLE_FLAGS_HORZ_ALIGN_CENTER = 2; export const STYLE_FLAGS_VERT_ALIGN_TOP = 0 << 3; export const STYLE_FLAGS_VERT_ALIGN_BOTTOM = 1 << 3; export const STYLE_FLAGS_VERT_ALIGN_CENTER = 2 << 3; export const STYLE_FLAGS_BLINK = 1 << 6; export function buildGuiStylesEnum(assets: Assets) { let styles = assets.styles.filter(style => style.id != undefined || style.alwaysBuild); const styleEnumItems = styles.map((style, i) => { return `${TAB}${getName( "STYLE_ID_", style.name ? style : "inline" + i, NamingConvention.UnderscoreUpperCase )}${style.id != undefined ? `= ${style.id}` : ""}`; }); styleEnumItems.unshift(`${TAB}STYLE_ID_NONE = 0`); return `enum StylesEnum {\n${styleEnumItems.join(",\n")}\n};`; } export function buildGuiStylesData(assets: Assets, dataBuffer: DataBuffer) { function buildStyle(style: Style) { // flags let flags = 0; let styleAlignHorizontal = style.alignHorizontalProperty; if (styleAlignHorizontal == "left") { flags |= STYLE_FLAGS_HORZ_ALIGN_LEFT; } else if (styleAlignHorizontal == "right") { flags |= STYLE_FLAGS_HORZ_ALIGN_RIGHT; } else { flags |= STYLE_FLAGS_HORZ_ALIGN_CENTER; } let styleAlignVertical = style.alignVerticalProperty; if (styleAlignVertical == "top") { flags |= STYLE_FLAGS_VERT_ALIGN_TOP; } else if (styleAlignVertical == "bottom") { flags |= STYLE_FLAGS_VERT_ALIGN_BOTTOM; } else { flags |= STYLE_FLAGS_VERT_ALIGN_CENTER; } let styleBlink = style.blinkProperty; if (styleBlink) { flags |= STYLE_FLAGS_BLINK; } dataBuffer.writeUint16(flags); // backgroundColor let backgroundColor = assets.getColorIndex(style, "backgroundColor"); if (isNaN(backgroundColor)) { backgroundColor = 0; } dataBuffer.writeUint16(backgroundColor); // color let color = assets.getColorIndex(style, "color"); if (isNaN(color)) { color = 0; } dataBuffer.writeUint16(color); // activeBackgroundColor let activeBackgroundColor = assets.getColorIndex( style, "activeBackgroundColor" ); if (isNaN(activeBackgroundColor)) { activeBackgroundColor = 0; } dataBuffer.writeUint16(activeBackgroundColor); // activeColor let activeColor = assets.getColorIndex(style, "activeColor"); if (isNaN(activeColor)) { activeColor = 0; } dataBuffer.writeUint16(activeColor); // focusBackgroundColor let focusBackgroundColor = assets.getColorIndex( style, "focusBackgroundColor" ); if (isNaN(focusBackgroundColor)) { focusBackgroundColor = 0; } dataBuffer.writeUint16(focusBackgroundColor); // focusColor let focusColor = assets.getColorIndex(style, "focusColor"); if (isNaN(focusColor)) { focusColor = 0; } dataBuffer.writeUint16(focusColor); // borderSize dataBuffer.writeUint8(style.borderSizeRect.top); dataBuffer.writeUint8(style.borderSizeRect.right); dataBuffer.writeUint8(style.borderSizeRect.bottom); dataBuffer.writeUint8(style.borderSizeRect.left); // borderColor let borderColor = assets.getColorIndex(style, "borderColor"); if (isNaN(borderColor)) { borderColor = 0; } dataBuffer.writeUint16(borderColor); // borderRadius dataBuffer.writeUint8(style.borderRadiusSpec.topLeftX || 0); dataBuffer.writeUint8(style.borderRadiusSpec.topLeftY || 0); dataBuffer.writeUint8(style.borderRadiusSpec.topRightX || 0); dataBuffer.writeUint8(style.borderRadiusSpec.topRightY || 0); dataBuffer.writeUint8(style.borderRadiusSpec.bottomRightX || 0); dataBuffer.writeUint8(style.borderRadiusSpec.bottomRightY || 0); dataBuffer.writeUint8(style.borderRadiusSpec.bottomLeftX || 0); dataBuffer.writeUint8(style.borderRadiusSpec.bottomLeftY || 0); // font, int8: in a project with a master project the fonts of the // project are negative let fontIndex = style.fontName ? assets.getFontIndex(style, "fontName") : 0; if (fontIndex < -128 || fontIndex > 127) { assets.projectStore.outputSectionsStore.write( Section.OUTPUT, MessageType.ERROR, `font index ${fontIndex} doesn't fit into the style font (-128 to 127)`, style ); fontIndex = 0; } dataBuffer.writeInt8(fontIndex); // opacity dataBuffer.writeUint8(style.opacityProperty); // padding dataBuffer.writeUint8(style.paddingRect.top); dataBuffer.writeUint8(style.paddingRect.right); dataBuffer.writeUint8(style.paddingRect.bottom); dataBuffer.writeUint8(style.paddingRect.left); // backgroundImage let backgroundImage: number = 0; if (style.backgroundImageProperty) { // inherited value, the style of the widget in the page layout // inherits it from the widget style backgroundImage = assets.getBitmapIndex( style, "backgroundImageProperty" ); } dataBuffer.writeInt16(backgroundImage); } // The index of a style is its position in assets.styles (id - 1 for a // style with an id), so a missing id leaves a hole. Keep the positions: // write the first style in place of a missing one. let styles: Style[] = []; if (!assets.projectStore.projectTypeTraits.isDashboard) { const firstStyle = assets.styles.find(style => !!style); styles = Array.from(assets.styles, style => style ?? firstStyle!); } dataBuffer.writeArray(styles, buildStyle); }