CabinGame/Luxe/blocks/ui/adventure.wren
2020-09-25 16:49:25 +02:00

205 lines
No EOL
6.7 KiB
Text

import "luxe: draw" for Draw, PathStyle
import "luxe: world" for Entity, Transform, UI, UIRenderMode, UIEvent
import "luxe: world" for UILayout, UILayoutBehave, UILayoutContain
import "luxe: ui/control" for Control
import "luxe: ui/label" for UILabel
import "luxe: assets" for Assets
import "luxe: render" for Material, TextAlign
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/ui" for Ui
import "blocks/ui/slider" for UiSlider
import "blocks/ui/compass" for UiCompass
import "math/math" for M
class UiAdventure{
root{_root}
static direction{"direction"}
static people{"people"}
static resources{"resources"}
static depart{"depart"}
construct new(ent, ui){
_ent = ent
_ui = ui
_game = Globals["Game"]
_page = Observable.new(UiAdventure.direction)
setup()
}
setup(){
var ui_rect = Globals["UiRect"]
var game = Globals["Game"]
_root = Control.create(_ent)
Control.set_allow_input(_root, true)
Control.set_size(_root, ui_rect.width, ui_rect.height)
toolbar()
direction()
people()
resources()
depart()
}
toolbar(){
//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) //|
//toolbar icons
_ui.list_button(list, [1, 0], "Info") {_ui.ui_mode = Ui.Info}
_ui.list_button(list, [0, 0], "Abort") {
_ui.ui_mode = Ui.Info
_game.adventures.discard()
_page.value = UiAdventure.direction
}
_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}
}
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) //|
_page.on_change(true){|val|
Control.set_visible(page, val == UiAdventure.direction)
}
//compass
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)
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
var angle = val.direction * 360 / steps
UiCompass.set_angle(compass, angle)
}
Control.set_events(compass){|event|
if(event.type == UIEvent.change){
var angle = event.change
var angleIndex = M.repeat((steps * angle / 360).round, steps)
var adventure = _game.adventures.planning
adventure.value.direction = angleIndex
adventure.emit()
}
}
//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)
_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)")
}
}
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)
}
}
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)
}
}
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)
}
}
}