diff --git a/src/main/java/com/reider745/block/CustomBlock.java b/src/main/java/com/reider745/block/CustomBlock.java index 9b8443de1..fc7375b69 100644 --- a/src/main/java/com/reider745/block/CustomBlock.java +++ b/src/main/java/com/reider745/block/CustomBlock.java @@ -58,7 +58,7 @@ public static void init(){ CustomManager manager = getBlockManager(id); ArrayList variants = getVariants(manager); for(int data = 0;data < variants.size();data++) - registerBlock(id, data, new CustomBlock(id, data, manager)); + registerBlock(id, data, new CustomBlock(id, data, manager, variants.get(data))); }); } @@ -85,7 +85,7 @@ public static ArrayList getVariants(CustomManager manager){ public static HashMap customBlocks = new HashMap<>(); - public static CustomManager registerBlock(String textId, int id, String name, Class item){ + public static CustomManager registerBlock(String textId, int id, String name, Class item){ CustomManager manager = new CustomManager(id, item, "block"); manager.put("name", name); @@ -111,11 +111,15 @@ protected CustomBlock(int id, int meta){ } protected CustomBlock(int id, int meta, CustomManager manager) { + this(id, meta, manager, "blank"); + } + + protected CustomBlock(int id, int meta, CustomManager manager, String name) { super(meta); this.manager = manager; this.id = id; - this.name = manager.get("name"); + this.name = name; TickingTile = manager.get("TickingTile:"+meta, false); } diff --git a/src/main/java/com/reider745/commands/CustomBlocksCommands.java b/src/main/java/com/reider745/commands/CustomBlocksCommands.java new file mode 100644 index 000000000..0a9ca8e3e --- /dev/null +++ b/src/main/java/com/reider745/commands/CustomBlocksCommands.java @@ -0,0 +1,27 @@ +package com.reider745.commands; + +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import com.reider745.api.CustomManager; +import com.reider745.block.CustomBlock; +import com.reider745.item.CustomItem; +import com.reider745.item.ItemMethod; + +public class CustomBlocksCommands extends Command { + public CustomBlocksCommands() { + super("mods", "get list custom blocks"); + } + + @Override + public boolean execute(CommandSender commandSender, String s, String[] strings) { + if(!commandSender.isOp()) return false; + + String message = "===CustomBlocks==="; + for(Integer id : CustomBlock.blocks.keySet()) { + CustomManager manager = CustomItem.getItemManager(id); + message += "\n"+manager.get(ItemMethod.PropertiesNames.NAME)+", id: "+id; + } + commandSender.sendMessage(message); + return true; + } +} diff --git a/src/main/java/com/reider745/commands/CustomItemsCommands.java b/src/main/java/com/reider745/commands/CustomItemsCommands.java new file mode 100644 index 000000000..d27eb77df --- /dev/null +++ b/src/main/java/com/reider745/commands/CustomItemsCommands.java @@ -0,0 +1,27 @@ +package com.reider745.commands; + +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import com.reider745.api.CustomManager; +import com.reider745.item.CustomItem; +import com.reider745.item.ItemMethod; + + +public class CustomItemsCommands extends Command { + public CustomItemsCommands() { + super("mods", "get list custom items"); + } + + @Override + public boolean execute(CommandSender commandSender, String s, String[] strings) { + if(!commandSender.isOp()) return false; + + String message = "===CustomItems==="; + for(Integer id : CustomItem.items.keySet()) { + CustomManager manager = CustomItem.getItemManager(id); + message += "\n"+manager.get(ItemMethod.PropertiesNames.NAME)+", id: "+id; + } + commandSender.sendMessage(message); + return true; + } +} diff --git a/src/main/java/com/reider745/hooks/ItemUtils.java b/src/main/java/com/reider745/hooks/ItemUtils.java index 7c56a2e32..c48cb0ba0 100644 --- a/src/main/java/com/reider745/hooks/ItemUtils.java +++ b/src/main/java/com/reider745/hooks/ItemUtils.java @@ -32,7 +32,7 @@ @Hooks(class_name = "cn.nukkit.item.Item") public class ItemUtils implements HookClass { - public static PointersStorage items_pointers = new PointersStorage<>("items", ItemExtraDataProvider::new); + public static final PointersStorage items_pointers = new PointersStorage<>("items", ItemExtraDataProvider::new); @Inject(signature = "(ILjava/lang/Integer;I[B)Lcn/nukkit/item/Item;") public static Item get(int id, Integer meta, int count, byte[] tags){ diff --git a/src/main/java/com/reider745/item/CustomItem.java b/src/main/java/com/reider745/item/CustomItem.java index 59aed14ff..b8f0d5277 100644 --- a/src/main/java/com/reider745/item/CustomItem.java +++ b/src/main/java/com/reider745/item/CustomItem.java @@ -53,8 +53,8 @@ private static void addFirst(ArrayList list, T value){ public static void checkAddedItem(int id, int damage){ if(!CustomItem.isCreativeItem(id, damage) && id > 2000) { - System.out.println(creative); - Server.getInstance().getLogger().info("clone item "+id+":"+damage); + //System.out.println(creative); + // Server.getInstance().getLogger().info("clone item "+id+":"+damage); CustomItem.addToCreativeGroup(CustomItem.TECHNICAL_GROUP, id); addFirst(CustomItem.creative, new int[] {id, 1, damage}); diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeBlockModel.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeBlockModel.java index 14760ef10..0cb51eed8 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeBlockModel.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeBlockModel.java @@ -108,7 +108,7 @@ public void addBox(float x1, float y1, float z1, float x2, float y2, float z2, S for (int i = 0; i < 6; i++) { if (!ResourcePackManager.isValidBlockTexture(names[i], ids[i])) { - Logger.debug(BlockRegistry.LOGGER_TAG, "invalid block texture will be replaced with default: " + names[i] + " " + ids[i]); + //Logger.debug(BlockRegistry.LOGGER_TAG, "invalid block texture will be replaced with default: " + names[i] + " " + ids[i]); names[i] = "missing_block"; ids[i] = 0; } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeItem.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeItem.java index e6f8c5d56..3ccc7ee3d 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeItem.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeItem.java @@ -168,7 +168,7 @@ public static NativeItem createArmorItem(int id, String nameId, String name, Str nameId = NativeAPI.convertNameId(nameId); // any name id must be lowercase if (!ResourcePackManager.isValidItemTexture(iconName, iconIndex)) { - Logger.debug("WARNING", "invalid item icon: " + iconName + " " + iconIndex); + //Logger.debug("WARNING", "invalid item icon: " + iconName + " " + iconIndex); iconName = "missing_icon"; iconIndex = 0; } @@ -187,7 +187,7 @@ public static NativeItem createThrowableItem(int id, String nameId, String name, nameId = NativeAPI.convertNameId(nameId); // any name id must be lowercase if (!ResourcePackManager.isValidItemTexture(iconName, iconIndex)) { - Logger.debug("WARNING", "invalid item icon: " + iconName + " " + iconIndex); + // Logger.debug("WARNING", "invalid item icon: " + iconName + " " + iconIndex); iconName = "missing_icon"; iconIndex = 0; } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java index 155d792e5..03a3f7734 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java @@ -2049,11 +2049,11 @@ public WindowGroup() { // legacy public static class StandartWindow extends UIWindowStandard { public StandartWindow(ScriptableObject content) { - + super(content); } public StandartWindow() { - + super(ScriptableObjectHelper.createEmpty()); } protected boolean isLegacyFormat() { @@ -2064,11 +2064,14 @@ protected boolean isLegacyFormat() { // new one public static class StandardWindow extends UIWindowStandard { public StandardWindow(ScriptableObject content) { + super(content); } public StandardWindow() { + super(ScriptableObjectHelper.createEmpty()); } + @Override protected boolean isLegacyFormat() { return false; } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/UIStyle.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/UIStyle.java new file mode 100644 index 000000000..764168301 --- /dev/null +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/UIStyle.java @@ -0,0 +1,293 @@ +package com.zhekasmirnov.innercore.api.mod.ui.types; + +import android.graphics.Color; +import org.mozilla.javascript.ScriptableObject; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; + +/** + * Created by zheka on 01.08.2017. + */ + +public class UIStyle { + public static final UIStyle DEFAULT = new UIStyle((UIStyle) null); + public static final UIStyle LEGACY = new UIStyle((UIStyle) null); + public static final UIStyle CLASSIC = DEFAULT; + + static { + LEGACY.setProperty("window_background", Color.rgb(0xc6, 0xc6, 0xc6)); + LEGACY.setProperty("header_background", Color.rgb(0xc6, 0xc6, 0xc6)); + LEGACY.setProperty("tab_background", Color.rgb(0x8c, 0x8c, 0x8c)); + LEGACY.setProperty("tab_background_selected", Color.rgb(0xc6, 0xc6, 0xc6)); + LEGACY.setProperty("default_font_color", Color.rgb(77, 77, 77)); + LEGACY.setProperty("default_font_shadow", 0.0f); + LEGACY.setProperty("close_button_size", 18); + LEGACY.setProperty("tab_frame_width", 2); + + LEGACY.addBinding("none", "_default_slot_empty"); + LEGACY.addBinding("slot", "_default_slot_light"); + LEGACY.addBinding("inv_slot", "_default_slot"); + LEGACY.addBinding("close_button_up", "close_button_up"); + LEGACY.addBinding("close_button_down", "close_button_down"); + LEGACY.addBinding("frame_background", "default_frame_bg"); + LEGACY.addBinding("frame_background_border", "default_frame_bg_border"); + LEGACY.addBinding("frame_background_light", "default_frame_bg_light"); + LEGACY.addBinding("frame_background_dark", "default_frame_bg_dark"); + LEGACY.addBinding("frame_tin", "default_frame_tin"); + LEGACY.addBinding("frame_tin_border", "default_frame_tin_border"); + LEGACY.addBinding("frame_tin_inverse", "default_frame_tin_inverse"); + LEGACY.addBinding("frame_slot", "default_frame_slot"); + LEGACY.addBinding("frame_slot_dark", "default_frame_slot_dark"); + LEGACY.addBinding("frame_input", "classic_frame_input"); + LEGACY.addBinding("frame_header", "default_header_frame"); + LEGACY.addBinding("frame_container", "default_container_frame"); + LEGACY.addBinding("frame_tab_left", "default_frame_tab_left"); + LEGACY.addBinding("frame_tab_right", "default_frame_tab_right"); + LEGACY.addBinding("button_up", "default_button_up"); + LEGACY.addBinding("button_hover", "default_button_up"); + LEGACY.addBinding("button_down", "default_button_down"); + LEGACY.addBinding("border_button_up", "default_border_button_up"); + LEGACY.addBinding("border_button_hover", "default_border_button_up"); + LEGACY.addBinding("border_button_down", "default_border_button_down"); + LEGACY.addBinding("selection", "_selection"); + + DEFAULT.setProperty("window_background", Color.rgb(0xc6, 0xc6, 0xc6)); + DEFAULT.setProperty("header_background", Color.rgb(0xc6, 0xc6, 0xc6)); + DEFAULT.setProperty("tab_background", Color.rgb(0x8c, 0x8c, 0x8c)); + DEFAULT.setProperty("tab_background_selected", Color.rgb(0xc6, 0xc6, 0xc6)); + DEFAULT.setProperty("default_font_color", Color.rgb(77, 77, 77)); + DEFAULT.setProperty("default_font_shadow", 0.0f); + DEFAULT.setProperty("close_button_size", 15); + DEFAULT.setProperty("tab_frame_width", 3); + DEFAULT.addBinding("slot", "classic_slot"); + DEFAULT.addBinding("inv_slot", "classic_slot"); + DEFAULT.addBinding("close_button_up", "classic_close_button"); + DEFAULT.addBinding("close_button_down", "classic_close_button_down"); + DEFAULT.addBinding("frame_background", "classic_frame_bg"); + DEFAULT.addBinding("frame_background_border", "classic_frame_bg_light_border"); + DEFAULT.addBinding("frame_background_light", "classic_frame_bg_light"); + DEFAULT.addBinding("frame_background_dark", "classic_frame_bg_dark"); + DEFAULT.addBinding("frame_tin", "classic_frame_tin"); + DEFAULT.addBinding("frame_tin_border", "classic_frame_tin_border"); + DEFAULT.addBinding("frame_tin_inverse", "classic_frame_tin_inverse"); + DEFAULT.addBinding("frame_slot", "classic_frame_slot"); + DEFAULT.addBinding("frame_slot_dark", "classic_frame_slot"); + DEFAULT.addBinding("frame_input", "classic_frame_input"); + DEFAULT.addBinding("frame_header", "classic_frame_bg_light"); + DEFAULT.addBinding("frame_container", "default_container_frame"); + DEFAULT.addBinding("frame_tab_left", "classic_frame_tab_left"); + DEFAULT.addBinding("frame_tab_right", "classic_frame_tab_right"); + DEFAULT.addBinding("button_up", "classic_button_up"); + DEFAULT.addBinding("button_hover", "classic_button_hover"); + DEFAULT.addBinding("button_down", "classic_button_down"); + DEFAULT.addBinding("border_button_up", "classic_border_button_up"); + DEFAULT.addBinding("border_button_hover", "classic_border_button_hover"); + DEFAULT.addBinding("selection", "_selection"); + } + + + private UIStyle parent = null; + private HashMap styleBindings = new HashMap<>(); + private ArrayList additionalStyles = new ArrayList<>(); + + public void addBinding(String key, String name) { + styleBindings.put(key, name); + } + + public String getBinding(String key, String fallback) { + for (UIStyle style : additionalStyles){ + if (style.styleBindings.containsKey(key)) { + return style.styleBindings.get(key); + } + } + + if (styleBindings.containsKey(key)) { + return styleBindings.get(key); + } + + if (parent != null){ + return parent.getBinding(key, fallback); + } + + return fallback; + } + + public void addStyle(UIStyle style) { + if (style != null) { + additionalStyles.add(style); + } + } + + private UIStyle(UIStyle style) { + inherit(style); + } + + public UIStyle() { + this(DEFAULT); + } + + public UIStyle(ScriptableObject obj) { + this(); + addAllBindings(obj); + } + + public UIStyle copy() { + UIStyle style = new UIStyle(parent); + for (String key : styleBindings.keySet()) { + style.addBinding(key, styleBindings.get(key)); + } + return style; + } + + public void inherit(UIStyle style) { + if (style != null) { + parent = style; + } + } + + public void addAllBindings(ScriptableObject obj) { + Object[] keys = obj.getAllIds(); + for (Object key : keys) { + String name = key.toString(); + Object val = obj.get(key); + if (val instanceof CharSequence) { + addBinding(name, val.toString()); + } + } + } + + public Collection getAllBindingNames() { + return styleBindings.keySet(); + } + + public String getBitmapName(String name) { + if (name.startsWith("style:")) { + name = name.substring(6); + return getBinding(name, name); + } + else { + return name; + } + } + + + private final HashMap properties = new HashMap<>(); + + public int getIntProperty(String name, int defaultValue) { + return properties.containsKey(name) ? ((Number) properties.get(name)).intValue() : (parent != null ? parent.getIntProperty(name, defaultValue) : defaultValue); + } + + public float getFloatProperty(String name, float defaultValue) { + return properties.containsKey(name) ? ((Number) properties.get(name)).floatValue() : (parent != null ? parent.getFloatProperty(name, defaultValue) : defaultValue); + } + + public double getDoubleProperty(String name, double defaultValue) { + return properties.containsKey(name) ? ((Number) properties.get(name)).doubleValue() : (parent != null ? parent.getDoubleProperty(name, defaultValue) : defaultValue); + } + + public String getStringProperty(String name, String defaultValue) { + return properties.containsKey(name) ? properties.get(name) + "" : (parent != null ? parent.getStringProperty(name, defaultValue) : defaultValue); + } + + public boolean getBooleanProperty(String name, boolean defaultValue) { + return properties.containsKey(name) ? ((Boolean) properties.get(name)).booleanValue() : (parent != null ? parent.getBooleanProperty(name, defaultValue) : defaultValue); + } + + public void setProperty(String name, Object value) { + properties.put(name, value); + } + + + + private static HashMap> resolveParameterString(String params) { + String[] parts = params.split("\\s"); + HashMap> result = new HashMap<>(); + + for (String part : parts) { + if (part.length() > 0) { + String[] argAndVals = part.split("="); + if (argAndVals.length == 2) { + String name = argAndVals[0]; + String[] values = argAndVals[1].split(","); + ArrayList valueList = new ArrayList<>(); + for (String val : values) { + if (val.length() > 0) { + valueList.add(val); + } + } + if (valueList.size() > 0) { + result.put(name, valueList); + } + } + } + } + + return result; + } + + + public static Object getBitmapByDescription(UIStyle style, String description) { + /* if (style == null) { + style = DEFAULT; + } + + int openBracket = description.indexOf('['); + if (openBracket != -1) { + int closeBracket = description.lastIndexOf(']'); + if (closeBracket != -1) { + String name = description.substring(0, openBracket); + HashMap> params = resolveParameterString(description.substring(openBracket + 1, closeBracket)); + + switch (name) { + case "frame": + if (params.containsKey("name")) { + String texName = params.get("name").get(0); + texName = style.getBitmapName(texName); + + FrameTexture texture = FrameTextureSource.getFrameTexture(texName); + ArrayList size = params.get("size"); + if (size != null && size.size() == 2) { + int width, height; + try { + width = Integer.parseInt(size.get(0)); + height = Integer.parseInt(size.get(1)); + } catch (NumberFormatException e) { + break; + } + + int color = texture.getCentralColor(); + if (params.containsKey("color")) { + String rawColor = params.get("color").get(0); + try { + color = Color.parseColor(rawColor); + } catch (IllegalArgumentException ignore) { + } + } + + boolean[] sides = new boolean[] {true, true, true, true}; + if (params.containsKey("sides")) { + ArrayList rawSides = params.get("sides"); + sides[FrameTexture.SIDE_TOP] = rawSides.contains("up"); + sides[FrameTexture.SIDE_BOTTOM] = rawSides.contains("down"); + sides[FrameTexture.SIDE_LEFT] = rawSides.contains("left"); + sides[FrameTexture.SIDE_RIGHT] = rawSides.contains("right"); + } + + return BitmapWrap.wrap(texture.expand(width, height, color, sides)); + } + } + + break; + } + } + } + else { + return BitmapWrap.wrap(style.getBitmapName(description)); + } + + return BitmapWrap.wrap("missing_bitmap");*/ + return null; + } +} diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/WindowContentAdapter.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/WindowContentAdapter.java new file mode 100644 index 000000000..80c903e85 --- /dev/null +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/types/WindowContentAdapter.java @@ -0,0 +1,72 @@ +package com.zhekasmirnov.innercore.api.mod.ui.types; + +import com.zhekasmirnov.innercore.api.mod.ScriptableObjectHelper; +import com.zhekasmirnov.innercore.api.mod.ui.window.UIWindowLocation; +import com.zhekasmirnov.innercore.mod.executable.Compiler; +import org.mozilla.javascript.IdFunctionObject; +import org.mozilla.javascript.NativeArray; +import org.mozilla.javascript.ScriptableObject; + +/** + * Created by zheka on 04.08.2017. + */ + +public class WindowContentAdapter { + ScriptableObject content, elements; + NativeArray drawing; + + public WindowContentAdapter() { + this(ScriptableObjectHelper.createEmpty()); + } + + public WindowContentAdapter(ScriptableObject content) { + super(); + if (content == null) { + content = ScriptableObjectHelper.createEmpty(); + } + + this.content = content; + + this.drawing = ScriptableObjectHelper.getNativeArrayProperty(this.content, "drawing", null); + if (this.drawing == null) { + this.drawing = ScriptableObjectHelper.createEmptyArray(); + this.content.put("drawing", content, this.drawing); + } + + this.elements = ScriptableObjectHelper.getScriptableObjectProperty(this.content, "elements", null); + if (this.elements == null) { + this.elements = ScriptableObjectHelper.createEmpty(); + this.content.put("elements", content, this.elements); + } + } + + public void addDrawing(ScriptableObject drawing) { + IdFunctionObject id = new IdFunctionObject(null, "Array", 8, 0); + this.drawing.execIdCall(id, Compiler.assureContextForCurrentThread(), this.drawing, this.drawing, new Object[]{drawing}); + } + + public void insertDrawing(ScriptableObject drawing) { + IdFunctionObject id = new IdFunctionObject(null, "Array", 11, 0); + this.drawing.execIdCall(id, Compiler.assureContextForCurrentThread(), this.drawing, this.drawing, new Object[]{drawing}); + } + + public void addElement(String name, ScriptableObject element) { + this.elements.put(name, this.elements, element); + } + + public ScriptableObject getDrawing() { + return drawing; + } + + public ScriptableObject getElements() { + return elements; + } + + public void setLocation(UIWindowLocation location) { + this.content.put("location", this.content, location.asScriptable()); + } + + public ScriptableObject getContent() { + return content; + } +} diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowGroup.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowGroup.java index 997f709fd..316c8c04b 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowGroup.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowGroup.java @@ -2,6 +2,7 @@ import com.zhekasmirnov.innercore.api.mod.ui.container.UiAbstractContainer; import com.zhekasmirnov.innercore.api.mod.ui.elements.UIElement; +import com.zhekasmirnov.innercore.api.mod.ui.types.UIStyle; import org.mozilla.javascript.ScriptableObject; import java.util.ArrayList; @@ -208,17 +209,17 @@ public void invalidateAllContent() { window.invalidateAllContent(); } } - - public void setStyle(Object style) { - + private UIStyle style; + public void setStyle(UIStyle style) { + this.style = style; } public void setStyle(ScriptableObject scriptable) { } - public Object getStyle() { - return null; + public UIStyle getStyle() { + return style; } public void setBlockingBackground(boolean val) { diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowStandard.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowStandard.java index 3c3adda2c..851bbd682 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowStandard.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/ui/window/UIWindowStandard.java @@ -1,4 +1,284 @@ package com.zhekasmirnov.innercore.api.mod.ui.window; +import android.graphics.Color; +import com.zhekasmirnov.innercore.api.mod.ScriptableObjectHelper; +import com.zhekasmirnov.innercore.api.mod.ui.TextureSource; +import com.zhekasmirnov.innercore.api.mod.ui.types.Font; +import com.zhekasmirnov.innercore.api.mod.ui.types.UIStyle; +import com.zhekasmirnov.innercore.api.mod.ui.types.WindowContentAdapter; +import org.mozilla.javascript.ScriptableObject; + +/** + * Created by zheka on 04.08.2017. + */ + public abstract class UIWindowStandard extends UIWindowGroup { + private ScriptableObject content; + private ScriptableObject standardContent; + + + public UIWindowStandard(ScriptableObject content) { + setContent(content); + setCloseOnBackPressed(true); + } + + private int paddingTop = 0; + private int paddingLeft = 0; + + + protected abstract boolean isLegacyFormat(); + + private void setupMainWindow() { + removeWindow("main"); + removeWindow("content"); + + WindowContentAdapter mainWinContent = new WindowContentAdapter(this.content); + + // setup location + UIWindowLocation loc = new UIWindowLocation(ScriptableObjectHelper.getScriptableObjectProperty(this.content, "location", null)); + loc.setPadding(UIWindowLocation.PADDING_TOP, paddingTop); + + // setup background + if (standardContent != null) { + // prepare height parameter + int minHeight = ScriptableObjectHelper.getIntProperty(standardContent, "minHeight", 0); + + // setup separate content window if required + ScriptableObject contentWindowData = ScriptableObjectHelper.getScriptableObjectProperty(standardContent, "contentWindow", !isLegacyFormat() ? ScriptableObjectHelper.createEmpty() : null); + if (contentWindowData != null) { + WindowContentAdapter contentWinContent = mainWinContent; + mainWinContent = new WindowContentAdapter(); + + int padding = ScriptableObjectHelper.getIntProperty(contentWindowData, "padding", 20); + int width = ScriptableObjectHelper.getIntProperty(contentWindowData, "width", 640); + + UIWindowLocation contentLoc = new UIWindowLocation(); + contentLoc.setPadding(UIWindowLocation.PADDING_LEFT, 1000 - width - padding); + contentLoc.setPadding(UIWindowLocation.PADDING_RIGHT, padding); + contentLoc.setPadding(UIWindowLocation.PADDING_TOP, paddingTop + padding + 20); + contentLoc.setPadding(UIWindowLocation.PADDING_BOTTOM, padding); + contentLoc.width = loc.scrollX = width; + if (minHeight > contentLoc.height) { + contentLoc.scrollY = (int) Math.max(contentLoc.height, width * minHeight / 1000f); + } + + contentWinContent.setLocation(contentLoc); + addWindow("content", contentWinContent.getContent()).setBackgroundColor(0); + } else if (minHeight > loc.height) { + loc.scrollY = minHeight; + } + + ScriptableObject background = ScriptableObjectHelper.getScriptableObjectProperty(standardContent, "background", null); + if (background != null) { + int color = ScriptableObjectHelper.getIntProperty(background, "color", Color.WHITE); + String bitmap = ScriptableObjectHelper.getStringProperty(background, "bitmap", null); + ScriptableObject frame = ScriptableObjectHelper.getScriptableObjectProperty(background, "frame", null); + + boolean isStandard = ScriptableObjectHelper.getBooleanProperty(background, "standard", ScriptableObjectHelper.getBooleanProperty(background, "standart", false)); + if (isStandard) { + color = getStyleSafe().getIntProperty("window_background", 0); + frame = ScriptableObjectHelper.createEmpty(); + } + + if (frame != null) { + ScriptableObject frameDrawing = ScriptableObjectHelper.createEmpty(); + frameDrawing.put("type", frameDrawing, "frame"); + frameDrawing.put("x", frameDrawing, 0); + frameDrawing.put("y", frameDrawing, 0); + frameDrawing.put("width", frameDrawing, loc.getWindowWidth()); + frameDrawing.put("height", frameDrawing, loc.getWindowHeight()); + frameDrawing.put("scale", frameDrawing, ScriptableObjectHelper.getFloatProperty(frame, "scale", 3)); + frameDrawing.put("bitmap", frameDrawing, ScriptableObjectHelper.getStringProperty(frame, "bitmap", getStyleSafe().getBinding("frame", "style:frame_background_border"))); + mainWinContent.insertDrawing(frameDrawing); + } + + if (bitmap != null) { + //Bitmap bmp = TextureSource.instance.getSafe(bitmap); + //float scale = 1000 / bmp.getWidth(); + float scale = 1; + + ScriptableObject imageDrawing = ScriptableObjectHelper.createEmpty(); + imageDrawing.put("type", imageDrawing, "bitmap"); + imageDrawing.put("x", imageDrawing, 0); + imageDrawing.put("y", imageDrawing, 0); + imageDrawing.put("scale", imageDrawing, scale); + imageDrawing.put("bitmap", imageDrawing, bitmap); + mainWinContent.insertDrawing(imageDrawing); + } + + ScriptableObject colorDrawing = ScriptableObjectHelper.createEmpty(); + colorDrawing.put("type", colorDrawing, "color"); + colorDrawing.put("color", colorDrawing, color); + mainWinContent.insertDrawing(colorDrawing); + } + + ScriptableObject header = ScriptableObjectHelper.getScriptableObjectProperty(standardContent, "header", null); + if (header != null && !ScriptableObjectHelper.getBooleanProperty(header, "hideShadow", false)) { + ScriptableObject shadow = ScriptableObjectHelper.createEmpty(); + shadow.put("type", shadow, "bitmap"); + shadow.put("x", shadow, 0); + shadow.put("y", shadow, 15); + shadow.put("scale", shadow, 2); + shadow.put("bitmap", shadow, "_standart_header_shadow"); + mainWinContent.addDrawing(shadow); + } + } + + mainWinContent.setLocation(loc); + + // setup window + addWindow("main", mainWinContent.getContent()); + moveOnTop("inventory"); + moveOnTop("content"); + moveOnTop("header"); + } + + private void setupHeader() { + paddingTop = 0; + removeWindow("header"); // cleanup + + if (standardContent == null) { + return; + } + + ScriptableObject header = ScriptableObjectHelper.getScriptableObjectProperty(standardContent, "header", null); + if (header == null) { + return; + } + + WindowContentAdapter content = new WindowContentAdapter(); + + int height = ScriptableObjectHelper.getIntProperty(header, "height", ScriptableObjectHelper.getIntProperty(header, "width", 80)); + paddingTop = height - 20; + + UIWindowLocation location = new UIWindowLocation(); + location.set(0, 0, 1000, height); + content.setLocation(location); + + String frameName = ScriptableObjectHelper.getStringProperty(header, "frame", getStyleSafe().getBinding("headerFrame", "style:frame_background_border")); + int frameColor = ScriptableObjectHelper.getIntProperty(header, "color", getStyleSafe().getIntProperty("header_background", 0)); + + ScriptableObject transparentBg = ScriptableObjectHelper.createEmpty(); + transparentBg.put("type", transparentBg, "color"); + transparentBg.put("color", transparentBg, Color.argb(0, 0, 0, 0)); + content.addDrawing(transparentBg); + + ScriptableObject frame = ScriptableObjectHelper.createEmpty(); + frame.put("type", frame, "frame"); + frame.put("x", frame, 0); + frame.put("y", frame, 0); + frame.put("width", frame, 1000); + frame.put("height", frame, height); + frame.put("bitmap", frame, frameName); + frame.put("color", frame, frameColor); + frame.put("scale", frame, 3); + content.addDrawing(frame); + + ScriptableObject textDescr = ScriptableObjectHelper.getScriptableObjectProperty(header, "text", null); + if (textDescr != null) { + ScriptableObject text = ScriptableObjectHelper.createEmpty(); + text.put("type", text, "text"); + text.put("x", text, 500); + text.put("y", text, height * .5f); + text.put("text", text, ScriptableObjectHelper.getStringProperty(textDescr, "text", "No Title")); + + ScriptableObject font = ScriptableObjectHelper.getScriptableObjectProperty(textDescr, "font", textDescr); + font.put("align", font, Font.ALIGN_CENTER); + font.put("size", font, ScriptableObjectHelper.getProperty(font, "size", height * .25f)); + font.put("color", font, ScriptableObjectHelper.getProperty(font, "color", getStyleSafe().getIntProperty("default_font_color", Color.BLACK))); + font.put("shadow", font, ScriptableObjectHelper.getProperty(font, "shadow", getStyleSafe().getFloatProperty("default_font_shadow", 0))); + text.put("font", text, font); + + content.addDrawing(text); + } + + boolean isButtonHidden = ScriptableObjectHelper.getBooleanProperty(header, "hideButton", false); + if (!isButtonHidden) { + ScriptableObject buttonStyle = ScriptableObjectHelper.getScriptableObjectProperty(header, "closeButton", null); + ScriptableObject button = ScriptableObjectHelper.createEmpty(); + button.put("type", button, "closeButton"); + button.put("x", button, 994 - height * .75); + button.put("y", button, 15); + button.put("scale", button, height / 18 * .75); + button.put("bitmap", button, getStyleSafe().getBitmapName("style:close_button_up")); + button.put("bitmap2", button, getStyleSafe().getBitmapName("style:close_button_down")); + content.addElement("default-close-button", button); + } + + addWindow("header", content.getContent()).setDynamic(false); + } + + private void setupInventory() { + paddingLeft = 0; + removeWindow("inventory"); // cleanup + + if (standardContent == null) { + return; + } + + ScriptableObject inventory = ScriptableObjectHelper.getScriptableObjectProperty(standardContent, "inventory", null); + if (inventory == null) { + return; + } + + int width = ScriptableObjectHelper.getIntProperty(inventory, "width", 300); + int padding = ScriptableObjectHelper.getIntProperty(inventory, "padding", 20); + + WindowContentAdapter content = new WindowContentAdapter(); + + UIWindowLocation loc = new UIWindowLocation(); + loc.setPadding(UIWindowLocation.PADDING_LEFT, paddingLeft + padding); + loc.setPadding(UIWindowLocation.PADDING_TOP, paddingTop + padding + 20); + loc.setPadding(UIWindowLocation.PADDING_BOTTOM, padding); + loc.width = loc.scrollX = width; + loc.scrollY = (int) Math.max(loc.height, width * 2.25f); + content.setLocation(loc); + + ScriptableObject background = ScriptableObjectHelper.createEmpty(); + background.put("type", background, "color"); + background.put("color", background, Color.BLACK); + content.addDrawing(background); + + int slotSize = 250; + for (int i = 0; i < 36; i++) { + ScriptableObject slot = ScriptableObjectHelper.createEmpty(); + slot.put("type", slot, "invSlot"); + slot.put("x", slot, (i % 4) * slotSize); + slot.put("y", slot, (i / 4) * slotSize); + slot.put("size", slot, slotSize + 1); + slot.put("index", slot, i); + content.addElement("__invSlot" + i, slot); + } + + UIWindow win = addWindow("inventory", content.getContent()); + //addWindowInstance("inventory", win); + win.setDynamic(false); + win.setInventoryNeeded(true); + } + + + + @Override + public ScriptableObject getContent() { + return content; + } + + public UIStyle getStyleSafe() { + UIStyle style = super.getStyle(); + return style != null ? style : UIStyle.DEFAULT; + } + + public void setContent(ScriptableObject content) { + this.content = content; + this.standardContent = ScriptableObjectHelper.getScriptableObjectProperty(content, "standard", ScriptableObjectHelper.getScriptableObjectProperty(content, "standart", null)); + + ScriptableObject style = ScriptableObjectHelper.getScriptableObjectProperty(content, "style", ScriptableObjectHelper.getScriptableObjectProperty(content, "params", null)); + if (style != null) { + setStyle(new UIStyle(style)); + } + + setupHeader(); + setupInventory(); + setupMainWindow(); + } }