From e1bdd74100faf62feb66f04fc355368c0f0e4233 Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Sun, 18 Feb 2024 13:56:45 -0300 Subject: [PATCH] =?UTF-8?q?Working=20option=20selection=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/style.css | 7 ++- src/components/DialogueBoxComponent.hx | 17 +++++- src/listeners/DialogueBoxController.hx | 81 ++++++-------------------- 3 files changed, 38 insertions(+), 67 deletions(-) diff --git a/res/style.css b/res/style.css index 70ce22a..645fc7e 100644 --- a/res/style.css +++ b/res/style.css @@ -66,11 +66,11 @@ dialogue-box { /* background-tile-pos: 1 1; */ background: blue; padding: 10; + alpha: 0.9; } dialogue-options { fill-width: true; - background: red; layout: vertical; } @@ -78,4 +78,9 @@ dialogue-option { fill-width: true; background: green; padding: 10; + alpha: 0.9; +} + +dialogue-option.disabled { + alpha: 0.5; } diff --git a/src/components/DialogueBoxComponent.hx b/src/components/DialogueBoxComponent.hx index 2f2d21f..ca4b754 100644 --- a/src/components/DialogueBoxComponent.hx +++ b/src/components/DialogueBoxComponent.hx @@ -1,6 +1,7 @@ package components; import dialogue.event.NextLine; +import dialogue.event.OptionSelected; import dialogue.event.OptionsShown.OptionChoice; @:uiComp("dialogue-option") @@ -19,9 +20,16 @@ class DialogueOptionComponent extends h2d.Flow implements h2d.domkit.Object { return s; } - public function new(label:String, ?parent) { + var choice:OptionChoice; + + public function new(choice:OptionChoice, ?parent) { super(parent); initComponent(); + this.choice = choice; + this.label = choice.text; + if (!choice.enabled) { + this.dom.addClass('disabled'); + } enableInteractive = true; interactive.onClick = function(_) onClick(); interactive.onOver = function(_) { @@ -38,7 +46,10 @@ class DialogueOptionComponent extends h2d.Flow implements h2d.domkit.Object { }; } - public dynamic function onClick() {} + public dynamic function onClick() { + if (this.choice.enabled) + Game.current.globalEventBus.publishEvent(new OptionSelected(this.choice.index)); + } } @:uiComp("dialogue-options") @@ -54,7 +65,7 @@ class DialogueOptionsComponent extends h2d.Flow implements h2d.domkit.Object { initComponent(); for (option in options) { this.options.push( - new DialogueOptionComponent(option.text, this) + new DialogueOptionComponent(option, this) ); } } diff --git a/src/listeners/DialogueBoxController.hx b/src/listeners/DialogueBoxController.hx index 764df38..1790cb9 100644 --- a/src/listeners/DialogueBoxController.hx +++ b/src/listeners/DialogueBoxController.hx @@ -15,7 +15,8 @@ import dialogue.event.NextLine; import components.DialogueBoxComponent; class DialogueBoxController { - public var curDialogue:DialogueBoxComponent; + public var dialogueBox:DialogueBoxComponent; + public var dialogueOptions:DialogueOptionsComponent; var game:Game; var bg:h2d.ScaleGrid; @@ -28,25 +29,10 @@ class DialogueBoxController { public function new(eventBus:EventBus, parent:h2d.Object, style:h2d.domkit.Style) { this.parent = parent; this.style = style; - // game = Game.current; - // parent.getScene().add(this, 1); - // bg = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5, this); - // bg.colorKey = 0xFF00FF; - // tf = new h2d.Text(game.font, this); - // tf.y = 5; - // tf.x = 7; - // tf.dropShadow = { - // dx: 0, - // dy: 1, - // color: 0, - // alpha: 0.3 - // }; - // int = new h2d.Interactive(0, 0, this); - // int.onClick = function(_) click(); - eventBus.subscribe(LineShown, dialogue); - eventBus.subscribe(OptionsShown, renderOptions); - // eventBus.subscribe(OptionSelected, onOptionSelected); + eventBus.subscribe(LineShown, onLineShown); + eventBus.subscribe(OptionsShown, onOptionsShown); + eventBus.subscribe(OptionSelected, onOptionSelected); eventBus.subscribe(DialogueComplete, onDialogueComplete); } @@ -61,59 +47,28 @@ class DialogueBoxController { public dynamic function onReady() {} - function dialogue(event:LineShown) { - if (curDialogue != null) { - curDialogue.remove(); + function onLineShown(event:LineShown) { + if (dialogueBox != null) { + dialogueBox.remove(); } var d = new DialogueBoxComponent(event.line(), this.parent); style.addObject(d); - curDialogue = d; + dialogueBox = d; } - function renderOptions(event:OptionsShown) { - new DialogueOptionsComponent(event.options, this.parent); + function onOptionsShown(event:OptionsShown) { + dialogueOptions = new DialogueOptionsComponent(event.options, this.parent); + } + + function onOptionSelected(event:OptionSelected) { + dialogueOptions.remove(); } - // - // function newPanel(w, h) { - // var g = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5); - // g.width = w; - // g.height = h; - // g.colorKey = 0xFF00FF; - // root.scene.getH2dObject().getScene().add(g, 1); - // return g; - // } - // - // function addOption(o:String, enabled:Bool, callback:() -> Void) { - // var spr = newPanel(Const.W, 60); - // curDialogue.addChild(spr); - // var tf = new h2d.Text(font, spr); - // if (!enabled) { - // tf.color = new Vector4(0.5, 0.5, 0.5, 1); - // } - // tf.text = o; - // tf.x = 10; - // tf.y = 10; - // spr.x = 0; - // spr.y = 100 + options.length * 60; - // var int = new h2d.Interactive(spr.width, spr.height, spr); - // int.onClick = function(_) { - // callback(); - // } - // int.cursor = Button; - // options.push(spr); - // return spr; - // } - // - // function onOptionSelected(event:OptionSelected) { - // for (o in options) { - // o.remove(); - // } - // options.splice(0, options.length); - // } function onDialogueComplete(event:DialogueComplete) { - curDialogue.remove(); + dialogueBox.remove(); + if (dialogueOptions != null) + dialogueOptions.remove(); } }