From 4a116d59876bd0f37c136f4307de1f8f60c7db17 Mon Sep 17 00:00:00 2001 From: Ronja Date: Sun, 13 Sep 2020 17:58:22 +0200 Subject: [PATCH] split ui into 2 files --- Luxe/assets/names.lx | 1 - Luxe/blocks/adventure.wren | 89 ++++++++++++++ Luxe/blocks/info.wren | 186 +++++++++++++++++++++++++++++ Luxe/blocks/ui.wren | 197 +++---------------------------- Luxe/blocks/ui/image_button.wren | 6 + Luxe/math/event.wren | 6 +- Luxe/math/observable.wren | 18 ++- Luxe/math/stringUtil.wren | 45 +++++++ 8 files changed, 355 insertions(+), 193 deletions(-) create mode 100644 Luxe/blocks/adventure.wren create mode 100644 Luxe/blocks/info.wren create mode 100644 Luxe/math/stringUtil.wren diff --git a/Luxe/assets/names.lx b/Luxe/assets/names.lx index 1faea76..c74a9f1 100644 --- a/Luxe/assets/names.lx +++ b/Luxe/assets/names.lx @@ -11,7 +11,6 @@ names = [ "Lauren" "Innes" "Rami" - "Mao" "Richard" "Jules" "Mercedes" diff --git a/Luxe/blocks/adventure.wren b/Luxe/blocks/adventure.wren new file mode 100644 index 0000000..4fd8cae --- /dev/null +++ b/Luxe/blocks/adventure.wren @@ -0,0 +1,89 @@ +import "luxe: draw" for Draw, PathStyle +import "luxe: world" for Entity, Transform, UI, UIRenderMode +import "luxe: world" for UILayout, UILayoutBehave, UILayoutContain +import "luxe: ui/control" for Control +import "luxe: ui/panel" for UIPanel +import "luxe: ui/list" for UIList +import "luxe: ui/button" for UIButton +import "luxe: ui/image" for UIImage +import "luxe: assets" for Assets +import "luxe: ui/text" for UIText +import "luxe: render" for Material + +import "globals" for Globals +import "blocks/ui/image_button" for ImageButton +import "math/observable" for Observable +import "blocks/ui/simple_text" for UISimpleText +import "blocks/ui" for Ui + +class UiAdventure{ + root{_root} + + construct new(ent, ui){ + _ent = ent + _ui = ui + + setup() + } + + setup(){ + var ui_rect = Globals["UiRect"] + var game = Globals["Game"] + + _root = Control.create(_ent) + Control.set_size(_root, ui_rect.width, ui_rect.height) + + //list + var list = Control.create(_ent) + Control.child_add(_root, list) + Control.set_size(list, 0, 16) + UILayout.set_behave(_ent, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom)//| + UILayout.set_margin(_ent, list, 0, 0, 0, 0) + UILayout.set_contain(_ent, list, UILayoutContain.row | UILayoutContain.start) //| + + var adventureButtons = Assets.image("assets/AdventureButtons") + var tiles = [10, 1] + var button + + //back to info + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Info") + ImageButton.set_tile_uv(button, tiles, [1, 0]) + ImageButton.set_state_change(button) { |data| + if(data["press"]){ + _ui.ui_mode = Ui.Info + } + } + + //abort + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Abort") + ImageButton.set_tile_uv(button, tiles, [0, 0]) + + //people + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "People") + ImageButton.set_tile_uv(button, tiles, [2, 0]) + + //stuff + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Stuff") + ImageButton.set_tile_uv(button, tiles, [4, 0]) + + //direction + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Direction") + ImageButton.set_tile_uv(button, tiles, [3, 0]) + + //go + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Depart") + ImageButton.set_tile_uv(button, tiles, [5, 0]) + } +} \ No newline at end of file diff --git a/Luxe/blocks/info.wren b/Luxe/blocks/info.wren new file mode 100644 index 0000000..2d601c3 --- /dev/null +++ b/Luxe/blocks/info.wren @@ -0,0 +1,186 @@ +import "luxe: draw" for Draw, PathStyle +import "luxe: world" for Entity, Transform, UI, UIRenderMode +import "luxe: world" for UILayout, UILayoutBehave, UILayoutContain +import "luxe: ui/control" for Control +import "luxe: ui/panel" for UIPanel +import "luxe: ui/list" for UIList +import "luxe: ui/button" for UIButton +import "luxe: ui/image" for UIImage +import "luxe: assets" for Assets +import "luxe: ui/text" for UIText +import "luxe: render" for Material + +import "globals" for Globals +import "blocks/ui/image_button" for ImageButton +import "math/observable" for Observable +import "math/stringUtil" for StringUtil +import "blocks/ui/simple_text" for UISimpleText +import "blocks/ui" for Ui +import "blocks/human/human" for Human + +class UiInfo{ + root{_root} + + static human{"human"} + static diary{"diary"} + static resources{"resources"} + + construct new(ent, ui){ + _ent = ent + _ui = ui + + _page = Observable.new(UiInfo.human) + + setup() + } + + setup(){ + var ui_rect = Globals["UiRect"] + + _root = Control.create(_ent) + UILayout.set_behave(_ent, _root, UILayoutBehave.fill) + UILayout.set_margin(_ent, _root, 0, 0, 0, 0) + + toolbar() + human() + diary() + } + + toolbar(){ + var ui_rect = Globals["UiRect"] + + //list + var list = Control.create(_ent) + Control.set_id(list, "info list") + Control.child_add(_root, list) + //make list span bottom area + UILayout.set_behave(_ent, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom) //| + UILayout.set_margin(_ent, list, 0, 0, 0, 0) + UILayout.set_contain(_ent, list, UILayoutContain.row | UILayoutContain.start) //| + Control.set_size(list, 16, 16) + + var adventureButtons = Assets.image("assets/AdventureButtons") + var tiles = [10, 1] + var button + + //plan adventure + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Adventure!") + ImageButton.set_tile_uv(button, tiles, [1, 0]) + ImageButton.set_state_change(button) { |data| + if(data["press"]){ + _ui.ui_mode = Ui.Planning + } + } + + //info + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Stats") + ImageButton.set_tile_uv(button, tiles, [2, 0]) + ImageButton.set_state_change(button) { |data| + if(data["press"]){ + _page.value = UiInfo.human + } + } + + //read diary + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Diary") + ImageButton.set_tile_uv(button, tiles, [1, 0]) + ImageButton.set_state_change(button) { |data| + if(data["press"]){ + _page.value = UiInfo.diary + } + } + + //resources + button = _ui.list_button(list) + UIImage.set_image(button, adventureButtons) + ImageButton.set_tooltip(button, "Resources") + ImageButton.set_tile_uv(button, tiles, [4, 0]) + ImageButton.set_state_change(button) { |data| + if(data["press"]){ + _page.value = UiInfo.resources + } + } + } + + human(){ + var page = Control.create(_ent) + Control.child_add(_root, page) + UILayout.set_behave(_ent, page, UILayoutBehave.fill) + UILayout.set_margin(_ent, page, 0, 0, 0, 16) + + _page.on_change(true){|val| + Control.set_visible(page, val == UiInfo.human) + } + + var portrait = UIImage.create(_ent) + Control.set_size(portrait, 30, 46) + Control.set_id(portrait, "person info portrait") + Control.child_add(page, portrait) + UILayout.set_behave(_ent, portrait, UILayoutBehave.left | UILayoutBehave.top) //| + UILayout.set_margin(_ent, portrait, 1, 1, 0, 0) + + var frame = UIImage.create(_ent) + UIImage.set_image(frame, Assets.image("assets/wip/Frame")) + Control.set_pos(frame, 1, 1) + Control.set_size(frame, 30, 46) + Control.set_id(frame, "person info frame") + Control.child_add(portrait, frame) + + Globals["Game"].Focus.on_change(true) {|val| + //todo: more sophisticated portrait generation + UIImage.set_image(portrait, Assets.image("assets/wip/Portrait")) + UIImage.set_color(portrait, Human.get_color(val) || [0, 0, 0, 1]) + } + + var statBlock = Control.create(_ent) + Control.child_add(page, statBlock) + UILayout.set_behave(_ent, statBlock, UILayoutBehave.fill) + UILayout.set_margin(_ent, statBlock, 33, 1, 0, 16) + UILayout.set_contain(_ent, statBlock, UILayoutContain.column | UILayoutContain.start) //| + + var name = UISimpleText.create(_ent) + Control.child_add(statBlock, name) + Control.set_id(name, "human name info") + Globals["Game"].Focus.on_change(true) {|val| + var name_string = Human.get_name(val) || "-" + UISimpleText.set_text(name, name_string) + } + + var adventures = UISimpleText.create(_ent) + Control.child_add(statBlock, adventures) + Control.set_id(name, "human adventure count") + Globals["Game"].Focus.on_change(true) {|val| + var count = Human.get_adventure_count(val) + var text = count ? "Adventures: %(count)" : "" + UISimpleText.set_text(adventures, text) + } + } + + diary(){ + var page = Control.create(_ent) + Control.child_add(_root, page) + UILayout.set_behave(_ent, page, UILayoutBehave.fill) + UILayout.set_margin(_ent, page, 0, 0, 0, 16) + + _page.on_change(true){|val| + Control.set_visible(page, val == UiInfo.diary) + } + + var title = UISimpleText.create(_ent) + Control.child_add(page, title) + Control.set_id(title, "human name info") + UILayout.set_behave(_ent, title, UILayoutBehave.left | UILayoutBehave.top) //| + UILayout.set_margin(_ent, title, 1, 1, 0, 0) + Globals["Game"].Focus.on_change(true) {|val| + var name = Human.get_name(val) + var diary_title = name ? "%(StringUtil.possesive(name)) Diary" : "Diary" + UISimpleText.set_text(title, diary_title) + } + } +} \ No newline at end of file diff --git a/Luxe/blocks/ui.wren b/Luxe/blocks/ui.wren index 2cb95e7..a217113 100644 --- a/Luxe/blocks/ui.wren +++ b/Luxe/blocks/ui.wren @@ -30,203 +30,36 @@ class Ui{ _ui_mode = Observable.new(Ui.Info) - _ui = Entity.create(app.ui, "UI Root") + _ent = Entity.create(app.ui, "UI Root") - UI.create(_ui, ui_rect.x, ui_rect.y, ui_rect.width, ui_rect.height, 0, app.ui_camera) - UI.set_render_mode(_ui, UIRenderMode.world) + UI.create(_ent, ui_rect.x, ui_rect.y, ui_rect.width, ui_rect.height, 0, app.ui_camera) + UI.set_render_mode(_ent, UIRenderMode.world) var solid_mat = Material.create("luxe: material_basis/ui_solid") var text_mat = Material.create("luxe: material_basis/ui_font") //var text_mat = Material.create("shaders/pixel_text_ui") - UI.set_material_basis(_ui, "luxe: material_basis/ui_solid", "shaders/pixel_text_ui") + UI.set_material_basis(_ent, "luxe: material_basis/ui_solid", "shaders/pixel_text_ui") - UILayout.create(_ui) + UILayout.create(_ent) - _info = setup_info() - _planning = setup_planning() + _info = UiInfo.new(_ent, this) + _planning = UiAdventure.new(_ent, this) _ui_mode.on_change(true) {|val| - Control.set_visible(_planning, val == Ui.Planning) - Control.set_visible(_info, val == Ui.Info) - UI.commit(_ui) + Control.set_visible(_planning.root, val == Ui.Planning) + Control.set_visible(_info.root, val == Ui.Info) + UI.commit(_ent) } game.Focus.on_change() {|val| ui_mode = Ui.Info } - UI.commit(_ui) - UILayout.commit(_ui) - } - - setup_info(){ - var ui_rect = Globals["UiRect"] - - var root = Control.create(_ui) - UILayout.set_behave(_ui, root, UILayoutBehave.fill) - UILayout.set_margin(_ui, root, 0, 0, 0, 0) - - setup_info_toolbar(root) - setup_info_human(root) - return root - } - - setup_info_toolbar(root){ - var ui_rect = Globals["UiRect"] - - //list - var list = Control.create(_ui) - Control.set_id(list, "info list") - Control.child_add(root, list) - //make list span bottom area - UILayout.set_behave(_ui, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom) - UILayout.set_margin(_ui, list, 0, 0, 0, 0) - UILayout.set_contain(_ui, list, UILayoutContain.row | UILayoutContain.start) - Control.set_size(list, 16, 16) - - var adventureButtons = Assets.image("assets/AdventureButtons") - var tiles = [10, 1] - var button - - //plan adventure - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Adventure!") - ImageButton.set_tile_uv(button, tiles, [1, 0]) - ImageButton.set_state_change(button) { |data| - if(data["press"]){ - ui_mode = Ui.Planning - } - } - - //info - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Stats") - ImageButton.set_tile_uv(button, tiles, [2, 0]) - - //read diary - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Diary") - ImageButton.set_tile_uv(button, tiles, [1, 0]) - - //resources - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Resources") - ImageButton.set_tile_uv(button, tiles, [4, 0]) - } - - setup_info_human(root){ - var portrait = UIImage.create(_ui) - Control.set_pos(portrait, 1, 1) - Control.set_size(portrait, 30, 46) - Control.set_id(portrait, "person info portrait") - Control.child_add(root, portrait) - UILayout.set_behave(_ui, portrait, UILayoutBehave.left | UILayoutBehave.top) - UILayout.set_margin(_ui, portrait, 1, 1, 0, 0) - - var frame = UIImage.create(_ui) - UIImage.set_image(frame, Assets.image("assets/wip/Frame")) - Control.set_pos(frame, 1, 1) - Control.set_size(frame, 30, 46) - Control.set_id(frame, "person info frame") - Control.child_add(portrait, frame) - - Globals["Game"].Focus.on_change(true) {|val| - //todo: more sophisticated portrait generation - UIImage.set_image(portrait, Assets.image("assets/wip/Portrait")) - UIImage.set_color(portrait, Human.get_color(val) || [0, 0, 0, 1]) - } - - var statBlock = Control.create(_ui) - UILayout.set_behave(_ui, statBlock, UILayoutBehave.fill) - UILayout.set_margin(_ui, statBlock, 33, 1, 0, 16) - UILayout.set_contain(_ui, statBlock, UILayoutContain.column | UILayoutContain.start) - - var name = UISimpleText.create(_ui) - Control.child_add(statBlock, name) - Control.set_id(name, "human name info") - Globals["Game"].Focus.on_change(true) {|val| - var name_string = Human.get_name(val) || "-" - UISimpleText.set_text(name, name_string) - } - - var adventures = UISimpleText.create(_ui) - Control.child_add(statBlock, adventures) - Control.set_id(name, "human adventure count") - Globals["Game"].Focus.on_change(true) {|val| - var count = Human.get_adventure_count(val) - var text = count ? "Adventures: %(count)" : "" - UISimpleText.set_text(adventures, text) - } - } - - setup_planning(){ - var ui_rect = Globals["UiRect"] - var game = Globals["Game"] - - var root = Control.create(_ui) - Control.set_size(root, ui_rect.width, ui_rect.height) - - //list - var list = Control.create(_ui) - Control.child_add(root, list) - Control.set_size(list, 0, 16) - UILayout.set_behave(_ui, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom) - UILayout.set_margin(_ui, list, 0, 0, 0, 0) - UILayout.set_contain(_ui, list, UILayoutContain.row | UILayoutContain.start) - - var adventureButtons = Assets.image("assets/AdventureButtons") - var tiles = [10, 1] - var button - - //back to info - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Info") - ImageButton.set_tile_uv(button, tiles, [1, 0]) - ImageButton.set_state_change(button) { |data| - if(data["press"]){ - ui_mode = Ui.Info - } - } - - //abort - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Abort") - ImageButton.set_tile_uv(button, tiles, [0, 0]) - - //people - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "People") - ImageButton.set_tile_uv(button, tiles, [2, 0]) - - //stuff - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Stuff") - ImageButton.set_tile_uv(button, tiles, [4, 0]) - - //direction - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Direction") - ImageButton.set_tile_uv(button, tiles, [3, 0]) - - //go - button = list_button(list) - UIImage.set_image(button, adventureButtons) - ImageButton.set_tooltip(button, "Depart") - ImageButton.set_tile_uv(button, tiles, [5, 0]) - - return root + UI.commit(_ent) + UILayout.commit(_ent) } list_button(parent){ - var button = ImageButton.create(_ui) + var button = ImageButton.create(_ent) Control.set_size(button, 16, 16) Control.child_add(parent, button) return button @@ -234,4 +67,6 @@ class Ui{ } //this is behind the class def to make cyclic dependency happy lol -import "blocks/human/human" for Human \ No newline at end of file +import "blocks/human/human" for Human +import "blocks/info" for UiInfo +import "blocks/adventure" for UiAdventure \ No newline at end of file diff --git a/Luxe/blocks/ui/image_button.wren b/Luxe/blocks/ui/image_button.wren index a330918..643b818 100644 --- a/Luxe/blocks/ui/image_button.wren +++ b/Luxe/blocks/ui/image_button.wren @@ -17,22 +17,28 @@ class ImageButton{ Control.set_events(button) {|event| var data = null + var string_type = "" if(event.type == UIEvent.enter){ + string_type = "enter" data = Control.get_state_data(button) data["hover"] = true } else if(event.type == UIEvent.exit) { + string_type = "exit" data = Control.get_state_data(button) data["hover"] = false data["press"] = false } else if(event.type == UIEvent.press) { + string_type = "press" data = Control.get_state_data(button) data["press"] = true } else if(event.type == UIEvent.release) { + string_type = "release" data = Control.get_state_data(button) data["press"] = false } if(data){ + System.print(string_type + " " + data["tooltip"]) var func = data["state_change"] if(func){ func.call(data, button) diff --git a/Luxe/math/event.wren b/Luxe/math/event.wren index c2475b5..2db3274 100644 --- a/Luxe/math/event.wren +++ b/Luxe/math/event.wren @@ -51,8 +51,12 @@ class Event{ emit(arguments){ _listeners.each{|val| - Util.call_arg_list(val, arguments) + Util.call_arg_list(val.value, arguments) } + _listenersOnce.each{|val| + Util.call_arg_list(val.value, arguments) + } + _listenersOnce.clear() } } diff --git a/Luxe/math/observable.wren b/Luxe/math/observable.wren index 39e7c70..73223f7 100644 --- a/Luxe/math/observable.wren +++ b/Luxe/math/observable.wren @@ -1,16 +1,14 @@ -import "luxe: events" for Events +import "math/event" for Event class Observable{ - static on_change{"on_change"} - value{_value} value=(v){ - _event.emit(Observable.on_change, v) + _event.emit([v]) _value = v } value_no_update=(v){_value = v} - events{_event} + event{_event} static new(value){ var variable = new() @@ -19,29 +17,29 @@ class Observable{ } construct new(){ - _event = Events.new() + _event = Event.new() } on_change(fn){ - _event.listen(Observable.on_change, fn) + return _event.listen(fn) } on_change(initial, fn){ if(initial){ fn.call(value) } - on_change(fn) + return on_change(fn) } not_on_change(fn){ - _event.unlisten(Observable.on_change, fn) + _event.unlisten(fn) } on_not_null(fn){ if(value){ fn.call(value) } else { - _event.once(Observable.on_change, fn) + _event.once(fn) } } } \ No newline at end of file diff --git a/Luxe/math/stringUtil.wren b/Luxe/math/stringUtil.wren new file mode 100644 index 0000000..0ccb40b --- /dev/null +++ b/Luxe/math/stringUtil.wren @@ -0,0 +1,45 @@ + + +class StringUtil{ + static possesive(string){ + if(string.endsWith("s")){ + return "%(string)'" + } else { + return "%(string)'s" + } + } + + static plural(string){ + if(string.endsWith("s")){ + return "%(string)'" + } else { + return "%(string)s" + } + } + + //todo: this doesnt cover all cases perfectly, add more + static a(string){ + var firstChar = string[0] + if(isVowel(firstChar)){ + return "an %(string)" + } else { + return "a %(string)" + } + } + + static isVowel(char){ + return "aeiou".contains(char) + } + + //todo: do + static capitalize(string){ + return string + } + + //todo: ths ignores words that arent preceeded by a space like (this) or "this". maybe fix that? + static capitalize_all(string){ + var words = string.split(" ") + words = words.map{|word| capitalize(word)} + return words.join(" ") + } +} \ No newline at end of file