CabinGame/Luxe/blocks/ui/adventure.wren

371 lines
13 KiB
Text
Raw Normal View History

2020-09-13 15:58:22 +00:00
import "luxe: draw" for Draw, PathStyle
2020-09-21 19:18:07 +00:00
import "luxe: world" for Entity, Transform, UI, UIRenderMode, UIEvent
2020-09-13 15:58:22 +00:00
import "luxe: world" for UILayout, UILayoutBehave, UILayoutContain
import "luxe: ui/control" for Control
import "luxe: ui/label" for UILabel
2020-10-04 16:35:12 +00:00
import "luxe: ui/image" for UIImage
2020-10-11 18:18:48 +00:00
import "luxe: assets" for Assets, Strings
import "luxe: render" for Material, TextAlign
2020-10-04 16:35:12 +00:00
import "luxe: color" for Color
import "luxe: game" for Frame
import "luxe: containers" for Lists
2020-09-13 15:58:22 +00:00
import "globals" for Globals
import "blocks/ui/clickable" for Clickable
import "blocks/ui/image_util" for UiImageUtil
2020-09-13 15:58:22 +00:00
import "math/observable" for Observable
import "blocks/ui/simple_text" for UISimpleText
2020-09-19 14:06:46 +00:00
import "blocks/ui/ui" for Ui
import "blocks/ui/slider" for UiSlider
2020-10-04 16:35:12 +00:00
import "blocks/ui/box" for UiBox
2020-10-11 15:22:57 +00:00
import "blocks/ui/scroll_box" for UiScrollBox
2020-09-21 19:18:07 +00:00
import "blocks/ui/compass" for UiCompass
2020-10-11 15:22:57 +00:00
import "blocks/ui/info" for UiInfo //this is a cyclic dependency waiting to happen...
2020-09-25 08:42:41 +00:00
import "math/math" for M
2020-10-04 16:35:12 +00:00
import "math/util" for Util
import "blocks/human/human" for Human
2020-09-13 15:58:22 +00:00
class UiAdventure{
root{_root}
2020-09-21 19:18:07 +00:00
static direction{"direction"}
static people{"people"}
static resources{"resources"}
static depart{"depart"}
2020-09-13 15:58:22 +00:00
construct new(ent, ui){
_ent = ent
_ui = ui
2020-09-21 19:18:07 +00:00
_game = Globals["Game"]
_page = Observable.new(UiAdventure.direction)
2020-09-13 15:58:22 +00:00
setup()
}
setup(){
var ui_rect = Globals["UiRect"]
var game = Globals["Game"]
_root = Control.create(_ent)
2020-09-21 19:18:07 +00:00
Control.set_allow_input(_root, true)
2020-09-13 15:58:22 +00:00
Control.set_size(_root, ui_rect.width, ui_rect.height)
2020-09-21 19:18:07 +00:00
toolbar()
direction()
people()
resources()
depart()
}
toolbar(){
2020-09-13 15:58:22 +00:00
//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) //|
2020-09-20 09:56:25 +00:00
//toolbar icons
_ui.list_button(list, [1, 0], "Info") {_ui.ui_mode = Ui.Info}
2020-10-04 16:35:12 +00:00
_ui.list_button(list, [3, 0], "Direction") {_page.value = UiAdventure.direction}
_ui.list_button(list, [2, 0], "People") {_page.value = UiAdventure.people}
_ui.list_button(list, [4, 0], "Stuff") {_page.value = UiAdventure.resources}
_ui.list_button(list, [5, 0], "Depart") {_page.value = UiAdventure.depart}
2020-09-21 19:18:07 +00:00
_ui.list_button(list, [0, 0], "Abort") {
_ui.ui_mode = Ui.Info
_game.adventures.discard()
2020-09-25 14:49:25 +00:00
_page.value = UiAdventure.direction
2020-09-21 19:18:07 +00:00
}
}
direction(){
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)
UILayout.set_contain(_ent, page, UILayoutContain.row | UILayoutContain.start) //|
2020-09-21 19:18:07 +00:00
_page.on_change(true){|val|
Control.set_visible(page, val == UiAdventure.direction)
}
//compass
2020-09-21 19:18:07 +00:00
var compass = UiCompass.create(_ent)
Control.child_add(page, compass)
Control.set_size(compass, 32, 32)
UILayout.set_margin(_ent, compass, 8, 0, 0, 0)
2020-09-25 08:42:41 +00:00
var steps = 16 //compass steps
var step_names = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
var full_step_names = step_names.map{|short|
var shorts = "NESW"
var base_directions = ["north","east","south","west"]
if(short.count == 1){
return base_directions[shorts.indexOf(short)]
} else if(short.count == 2) {
return base_directions[shorts.indexOf(short[0])] +
base_directions[shorts.indexOf(short[1])]
} else { //assume 3
return base_directions[shorts.indexOf(short[0])] + "-" +
base_directions[shorts.indexOf(short[1])] +
base_directions[shorts.indexOf(short[2])]
}
}.toList
_game.adventures.planning.on_change(true){|val|
if(val == null) return
2020-10-06 20:26:22 +00:00
var angle = -val.direction * 360 / steps
2020-09-25 08:42:41 +00:00
UiCompass.set_angle(compass, angle)
}
2020-09-21 19:18:07 +00:00
Control.set_events(compass){|event|
if(event.type == UIEvent.change){
2020-09-25 08:42:41 +00:00
var angle = event.change
var angleIndex = M.mod(-(steps * angle / 360).round, steps)
2020-09-25 08:42:41 +00:00
var adventure = _game.adventures.planning
adventure.value.direction = angleIndex
adventure.emit()
2020-09-21 19:18:07 +00:00
}
}
//other parent
var colParent = Control.create(_ent)
Control.set_id(colParent, "dir col parent")
Control.child_add(page, colParent)
UILayout.set_behave(_ent, colParent, UILayoutBehave.fill)
UILayout.set_margin(_ent, colParent, 8, 0, 0, 0)
UILayout.set_contain(_ent, colParent, UILayoutContain.column | UILayoutContain.start) //|
//slider
var slider = UiSlider.create(_ent)
Control.set_id(slider, "dir dist slider")
Control.child_add(colParent, slider)
Control.set_size(slider, 0, 11)
UILayout.set_behave(_ent, slider, UILayoutBehave.left | UILayoutBehave.right) //|
UILayout.set_margin(_ent, slider, 4, 4, 4, 0)
_game.adventures.planning.on_change(true){|val|
if(val == null) return
var relative_distance = val.distance / _game.adventures.max_distance
UiSlider.set_value(slider, relative_distance)
}
Control.set_events(slider){|event|
if(event.type == UIEvent.change){
var distance = (event.change * _game.adventures.max_distance).round
var adventure = _game.adventures.planning
adventure.value.distance = distance
adventure.emit()
}
}
//text
var text = UILabel.create(_ent)
Control.set_id(text, "dir text")
Control.child_add(colParent, text)
UILabel.set_font(text, _ui.font)
UILabel.set_text_size(text, 8)
UILabel.set_align_vertical(text, TextAlign.top)
UILayout.set_behave(_ent, text, UILayoutBehave.left) //|
UILayout.set_margin(_ent, text, 4, 8, 4, 0)
2020-09-25 14:49:25 +00:00
_game.adventures.planning.on_change(true) { |val|
if(val == null) return
//this stuff does bad things
var dir = full_step_names[val.direction]
var dist = val.distance
UILabel.set_text(text, "%(dist)km\n%(dir)")
}
2020-09-21 19:18:07 +00:00
}
people(){
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 == UiAdventure.people)
}
2020-10-04 16:35:12 +00:00
_game.focus.on_change(true) {|val|
if(!val) return
if(_ui.ui_mode == Ui.Planning && _page.value == UiAdventure.people){
var adventure = _game.adventures.planning
if(adventure.value && !adventure.value.adventurers.contains(val)) {
adventure.value.adventurers.insert(0, val)
adventure.emit()
}
}
}
2020-10-11 15:22:57 +00:00
var list = UiScrollBox.create(_ent)
2020-10-04 16:35:12 +00:00
Control.child_add(page, list)
UILayout.set_behave(_ent, list, UILayoutBehave.fill)
UILayout.set_margin(_ent, list, 40, 0, 0, 0)
var tiny_head = Assets.image("assets/wip/8Head")
var x_image = Assets.image("assets/wip/8Cross")
_adventurerListItems = []
_game.adventures.planning.on_change(true) {|adventure|
//cleanup
_adventurerListItems.each{|item|
Control.destroy(item)
}
_adventurerListItems.clear()
if(!adventure) return
//rebuild
2020-10-11 15:22:57 +00:00
var i = -1
2020-10-04 16:35:12 +00:00
adventure.adventurers.each{ |adventurer|
2020-10-11 15:22:57 +00:00
i = i + 1
2020-10-04 16:35:12 +00:00
var item = UiBox.create(_ent)
UILayout.set_behave(_ent, item, UILayoutBehave.left | UILayoutBehave.right)//|
UILayout.set_margin(_ent, item, 0, 0, 0, 0)
UILayout.set_contain(_ent, item, UILayoutContain.row | UILayoutContain.start)//|
Control.set_size(item, -1, 12)
2020-10-11 18:18:48 +00:00
Control.set_id(item, "Person %(i) : %(Human.get_name(adventurer))")
Clickable.make_clickable(item)
Clickable.set_on_click(item) {
_ui.ui_mode = Ui.Info
_ui.info.page.value = UiInfo.human
_game.focus.value = adventurer
2020-10-04 16:35:12 +00:00
}
2020-10-11 15:22:57 +00:00
UiScrollBox.add(list, item)
2020-10-04 16:35:12 +00:00
var head = UIImage.create(_ent)
Control.child_add(item, head)
Control.set_size(head, 8, 8)
UIImage.set_image(head, tiny_head)
UIImage.set_color(head, Human.get_color(adventurer))
UILayout.set_margin(_ent, head, 2, 0, 0, 0)
var name = UILabel.create(_ent)
Control.child_add(item, name)
UILabel.set_align_vertical(name, TextAlign.bottom)
UILabel.set_font(name, _ui.font)
UILabel.set_text_size(name, 8)
UILabel.set_text(name, Human.get_name(adventurer))
UILayout.set_behave(_ent, name, UILayoutBehave.hfill | UILayoutBehave.left)//|
UILayout.set_margin(_ent, name, 2, 1, 0, 1)
var remove = UIImage.create(_ent)
Clickable.make_clickable(remove)
2020-10-04 16:35:12 +00:00
Control.child_add(item, remove)
Control.set_size(remove, 8, 8)
UIImage.set_image(remove, x_image)
UIImage.set_color(remove, Color.hex(0xec172a))
UILayout.set_margin(_ent, remove, 0, 0, 2, 0)
Clickable.set_tooltip(remove, "remove")
Clickable.set_on_click(remove) {
Frame.end{
Globals["Tooltip"].clear(remove)
Lists.remove(adventure.adventurers, adventurer)
_game.adventures.planning.emit()
2020-10-04 16:35:12 +00:00
}
}
_adventurerListItems.add(item)
}
UILayout.commit(_ent)
UI.commit(_ent)
}
2020-09-21 19:18:07 +00:00
}
resources(){
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 == UiAdventure.resources)
}
2020-10-11 15:22:57 +00:00
var list = UiScrollBox.create(_ent)
Control.child_add(page, list)
UILayout.set_behave(_ent, list, UILayoutBehave.fill)
UILayout.set_margin(_ent, list, 40, 0, 0, 0)
var tiny_head = Assets.image("assets/wip/8Head")
var x_image = Assets.image("assets/wip/8Cross")
_itemListItems = []
_game.adventures.planning.on_change(true) {|adventure|
//cleanup
_itemListItems.each{|item|
Control.destroy(item)
}
_itemListItems.clear()
if(!adventure) return
//rebuild
adventure.adventurers.each{ |adventurer|
var item = UiBox.create(_ent)
UILayout.set_behave(_ent, item, UILayoutBehave.left | UILayoutBehave.right)//|
UILayout.set_margin(_ent, item, 0, 0, 0, 0)
UILayout.set_contain(_ent, item, UILayoutContain.row | UILayoutContain.start)//|
Control.set_size(item, -1, 12)
Clickable.make_clickable(item)
Clickable.set_on_click(item) {
_ui.ui_mode = Ui.Info
_ui.info.page.value = UiInfo.human
_game.focus.value = adventurer
2020-10-11 15:22:57 +00:00
}
UiScrollBox.add(list, item)
var head = UIImage.create(_ent)
Control.child_add(item, head)
Control.set_size(head, 8, 8)
UIImage.set_image(head, tiny_head)
UIImage.set_color(head, Human.get_color(adventurer))
UILayout.set_margin(_ent, head, 2, 0, 0, 0)
var name = UILabel.create(_ent)
Control.child_add(item, name)
UILabel.set_align_vertical(name, TextAlign.bottom)
UILabel.set_font(name, _ui.font)
UILabel.set_text_size(name, 8)
UILabel.set_text(name, Human.get_name(adventurer))
UILayout.set_behave(_ent, name, UILayoutBehave.hfill | UILayoutBehave.left)//|
UILayout.set_margin(_ent, name, 2, 1, 0, 1)
var remove = UIImage.create(_ent)
Clickable.make_clickable(remove)
2020-10-11 15:22:57 +00:00
Control.child_add(item, remove)
Control.set_size(remove, 8, 8)
UIImage.set_image(remove, x_image)
UIImage.set_color(remove, Color.hex(0xec172a))
UILayout.set_margin(_ent, remove, 0, 0, 2, 0)
Clickable.set_tooltip(remove, "remove")
Clickable.set_on_click(remove) {
Frame.end{
Globals["Tooltip"].clear(remove)
Lists.remove(adventure.adventurers, adventurer)
_game.adventures.planning.emit()
2020-10-11 15:22:57 +00:00
}
}
_itemListItems.add(item)
}
UILayout.commit(_ent)
UI.commit(_ent)
}
2020-09-21 19:18:07 +00:00
}
depart(){
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 == UiAdventure.depart)
}
2020-09-13 15:58:22 +00:00
}
}