summaryrefslogtreecommitdiff
path: root/play.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-10-19 01:31:31 +0200
committerTor Andersson <tor@ccxvii.net>2023-10-20 19:06:35 +0200
commit89d05a73e3e91ad727174f1d9ac6cb720e460332 (patch)
tree735b2e6e70fcac092e5ac18203d50f8e14229973 /play.js
parent2a9f4e4e5bab2345e12d43ae7bbca307c829091b (diff)
downloadwilderness-war-89d05a73e3e91ad727174f1d9ac6cb720e460332.tar.gz
Mobile layout.
Diffstat (limited to 'play.js')
-rw-r--r--play.js92
1 files changed, 60 insertions, 32 deletions
diff --git a/play.js b/play.js
index 45904b3..882fece 100644
--- a/play.js
+++ b/play.js
@@ -701,37 +701,68 @@ function on_blur_card(evt) {
// CARD MENU
-const card_action_menu = [
- 'play_event',
- 'activate_force',
- 'activate_individually',
- 'construct_stockades',
- 'construct_forts',
- 'discard',
-]
+var card_action_menu = Array.from(document.getElementById("popup").querySelectorAll("li[data-action]")).map(e => e.dataset.action)
+
+function is_popup_menu_action(menu_id, target_id) {
+ let menu = document.getElementById(menu_id)
+ for (let item of menu.querySelectorAll("li")) {
+ let action = item.dataset.action
+ if (action)
+ return true
+ }
+ return false
+}
+
+function show_popup_menu(evt, menu_id, target_id, title) {
+ let menu = document.getElementById(menu_id)
+
+ let show = false
+ for (let item of menu.querySelectorAll("li")) {
+ let action = item.dataset.action
+ if (action) {
+ if (is_action(action, target_id)) {
+ show = true
+ item.classList.add("action")
+ item.classList.remove("disabled")
+ item.onclick = function () {
+ send_action(action, target_id)
+ hide_popup_menu()
+ evt.stopPropagation()
+ }
+ } else {
+ item.classList.remove("action")
+ item.classList.add("disabled")
+ item.onclick = null
+ }
+ }
+ }
+
+ if (show) {
+ menu.onmouseleave = hide_popup_menu
+ menu.style.display = "block"
+ if (title) {
+ let item = menu.querySelector("li.title")
+ if (item) {
+ item.onclick = hide_popup_menu
+ item.textContent = title
+ }
+ }
-let current_popup_card = 0
+ let w = menu.clientWidth
+ let h = menu.clientHeight
+ let x = Math.max(5, Math.min(evt.clientX - w / 2, window.innerWidth - w - 5))
+ let y = Math.max(5, Math.min(evt.clientY - 12, window.innerHeight - h - 40))
+ menu.style.left = x + "px"
+ menu.style.top = y + "px"
-function show_popup_menu(evt, list) {
- document.querySelectorAll("#popup div").forEach(e => e.classList.remove('enabled'))
- for (let item of list) {
- let e = document.getElementById("menu_" + item)
- e.classList.add('enabled')
+ return true
}
- let popup = document.getElementById("popup")
- popup.style.display = 'block'
- popup.style.left = (evt.clientX-50) + "px"
- popup.style.top = (evt.clientY-12) + "px"
- cards[current_popup_card].element.classList.add("selected")
+
+ return false
}
function hide_popup_menu() {
- let popup = document.getElementById("popup")
- popup.style.display = 'none'
- if (current_popup_card) {
- cards[current_popup_card].element.classList.remove("selected")
- current_popup_card = 0
- }
+ document.getElementById("popup").style.display = "none"
}
function is_card_enabled(card) {
@@ -744,20 +775,17 @@ function is_card_enabled(card) {
return false
}
-function is_card_action(action, card) {
+function is_action(action, card) {
return view.actions && view.actions[action] && view.actions[action].includes(card)
}
function on_click_card(evt) {
let card = evt.target.card
- if (is_card_action('card', card)) {
+ if (is_action('card', card)) {
send_action('card', card)
} else {
- let menu = card_action_menu.filter(a => is_card_action(a, card))
- if (menu.length > 0) {
- current_popup_card = card
- show_popup_menu(evt, menu)
- }
+ show_popup_menu(evt, "popup", card, cards[card].name)
+ evt.stopPropagation()
}
}