From fd265b37d6241ed1c5f2d2d0dab56e4b546fd362 Mon Sep 17 00:00:00 2001 From: Robert Heinrich Date: Fri, 29 Mar 2013 01:48:45 +0100 Subject: [PATCH] Added lua GUI front-end for autobutcher. --- plugins/CMakeLists.txt | 2 +- plugins/lua/zone.lua | 12 + plugins/zone.cpp | 311 +++++++++++++++++++++- scripts/gui/autobutcher.lua | 496 ++++++++++++++++++++++++++++++++++++ 4 files changed, 819 insertions(+), 2 deletions(-) create mode 100644 plugins/lua/zone.lua create mode 100644 scripts/gui/autobutcher.lua diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index c21e50434..3d38616ea 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -112,7 +112,7 @@ if (BUILD_SUPPORTED) DFHACK_PLUGIN(tweak tweak.cpp) DFHACK_PLUGIN(feature feature.cpp) DFHACK_PLUGIN(lair lair.cpp) - DFHACK_PLUGIN(zone zone.cpp) + DFHACK_PLUGIN(zone zone.cpp LINK_LIBRARIES lua) DFHACK_PLUGIN(catsplosion catsplosion.cpp) DFHACK_PLUGIN(regrass regrass.cpp) DFHACK_PLUGIN(forceequip forceequip.cpp) diff --git a/plugins/lua/zone.lua b/plugins/lua/zone.lua new file mode 100644 index 000000000..f6a136690 --- /dev/null +++ b/plugins/lua/zone.lua @@ -0,0 +1,12 @@ +local _ENV = mkmodule('plugins.zone') + +--[[ + + Native functions: + + * autobutcher_isEnabled() + * autowatch_isEnabled() + +--]] + +return _ENV \ No newline at end of file diff --git a/plugins/zone.cpp b/plugins/zone.cpp index 3195cffda..8907e507b 100644 --- a/plugins/zone.cpp +++ b/plugins/zone.cpp @@ -40,6 +40,11 @@ using namespace std; #include "Console.h" #include "Export.h" #include "PluginManager.h" +#include "MiscUtils.h" + +#include "LuaTools.h" +#include "DataFuncs.h" + #include "modules/Units.h" #include "modules/Maps.h" #include "modules/Gui.h" @@ -51,7 +56,7 @@ using namespace std; #include "MiscUtils.h" #include -#include +#include "df/ui.h" #include "df/world.h" #include "df/world_raws.h" #include "df/building_def.h" @@ -2716,6 +2721,8 @@ bool compareUnitAgesOlder(df::unit* i, df::unit* j) return (age_i > age_j); } + + //enum WatchedRaceSubtypes //{ // femaleKid=0, @@ -2892,6 +2899,17 @@ public: // to ignore them for a while but still keep the target count settings std::vector watched_races; +// helper for sorting the watchlist alphabetically +bool compareRaceNames(WatchedRace* i, WatchedRace* j) +{ + string name_i = getRaceName(i->raceId); + string name_j = getRaceName(j->raceId); + + return (name_i < name_j); +} + +static void autobutcher_sortWatchList(color_ostream &out); + // default target values for autobutcher static int default_fk = 5; static int default_mk = 1; @@ -3282,6 +3300,7 @@ command_result df_autobutcher(color_ostream &out, vector & parameters) WatchedRace * w = new WatchedRace(watch_race, target_raceids.back(), target_fk, target_mk, target_fa, target_ma); w->UpdateConfig(out); watched_races.push_back(w); + autobutcher_sortWatchList(out); } target_raceids.pop_back(); } @@ -3367,6 +3386,7 @@ command_result autoButcher( color_ostream &out, bool verbose = false ) announce = "New race added to autobutcher watchlist: " + getRaceName(w->raceId); Gui::showAnnouncement(announce, 2, false); //out << announce << endl; + autobutcher_sortWatchList(out); } } @@ -3475,6 +3495,7 @@ command_result init_autobutcher(color_ostream &out) w->rconfig = *p; watched_races.push_back(w); } + autobutcher_sortWatchList(out); return CR_OK; } @@ -3541,6 +3562,294 @@ command_result cleanup_autonestbox(color_ostream &out) return CR_OK; } +///////////////////////////////////// +// API functions to control autobutcher with a lua script + +static bool autobutcher_isEnabled() { return enable_autobutcher; } +static bool autowatch_isEnabled() { return enable_autobutcher_autowatch; } + +static size_t autobutcher_getSleep(color_ostream &out) +{ + return sleep_autobutcher; +} + +static void autobutcher_setSleep(color_ostream &out, size_t ticks) +{ + sleep_autobutcher = ticks; + if(config_autobutcher.isValid()) + config_autobutcher.ival(1) = sleep_autobutcher; +} + +static void autobutcher_setEnabled(color_ostream &out, bool enable) +{ + if(enable) + { + enable_autobutcher = true; + start_autobutcher(out); + autoButcher(out, false); + } + else + { + enable_autobutcher = false; + if(config_autobutcher.isValid()) + config_autobutcher.ival(0) = enable_autobutcher; + out << "Autobutcher stopped." << endl; + } +} + +static void autowatch_setEnabled(color_ostream &out, bool enable) +{ + if(enable) + { + out << "Auto-adding to watchlist started." << endl; + enable_autobutcher_autowatch = true; + if(config_autobutcher.isValid()) + config_autobutcher.ival(2) = enable_autobutcher_autowatch; + } + else + { + out << "Auto-adding to watchlist stopped." << endl; + enable_autobutcher_autowatch = false; + if(config_autobutcher.isValid()) + config_autobutcher.ival(2) = enable_autobutcher_autowatch; + } +} + +static size_t autobutcher_getWatchListSize() +{ + return watched_races.size(); +} + +// get race name for a watchlist index +static std::string autobutcher_getWatchListRace(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return "INVALID"; + + WatchedRace * w = watched_races[idx]; + return getRaceName(w->raceId); +} + +// get FK for a watchlist index +static size_t autobutcher_getWatchListRaceFK(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return -1; + + WatchedRace * w = watched_races[idx]; + return w->fk; +} + +// get FA for a watchlist index +static size_t autobutcher_getWatchListRaceFA(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return -1; + + WatchedRace * w = watched_races[idx]; + return w->fa; +} + +// get MK for a watchlist index +static size_t autobutcher_getWatchListRaceMK(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return -1; + + WatchedRace * w = watched_races[idx]; + return w->mk; +} + +// get MA for a watchlist index +static size_t autobutcher_getWatchListRaceMA(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return -1; + + WatchedRace * w = watched_races[idx]; + return w->ma; +} + +// set FK for a watchlist index +static void autobutcher_setWatchListRaceFK(color_ostream &out, size_t idx, size_t value) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->fk = value; + w->UpdateConfig(out); +} + +// set FA for a watchlist index +static void autobutcher_setWatchListRaceFA(color_ostream &out, size_t idx, size_t value) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->fa = value; + w->UpdateConfig(out); +} + +// set MK for a watchlist index +static void autobutcher_setWatchListRaceMK(color_ostream &out, size_t idx, size_t value) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->mk = value; + w->UpdateConfig(out); +} + +// set MA for a watchlist index +static void autobutcher_setWatchListRaceMA(color_ostream &out, size_t idx, size_t value) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->ma = value; + w->UpdateConfig(out); +} + +// check if "watch" is enabled for watchlist index +static bool autobutcher_isWatchListRaceWatched(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return false; + + WatchedRace * w = watched_races[idx]; + return w->isWatched; +} + +// set "watched" status for a watchlist index +static void autobutcher_setWatchListRaceWatched(color_ostream &out, size_t idx, bool watched) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->isWatched = watched; + w->UpdateConfig(out); +} + +// remove entry from watchlist +static void autobutcher_removeFromWatchList(color_ostream &out, size_t idx) +{ + if(idx >= watched_races.size()) + return; + + WatchedRace * w = watched_races[idx]; + w->RemoveConfig(out); + watched_races.erase(watched_races.begin()+idx); +} + +// sort watchlist alphabetically +static void autobutcher_sortWatchList(color_ostream &out) +{ + sort(watched_races.begin(), watched_races.end(), compareRaceNames); +} + +// get default target values for new races +static size_t autobutcher_getDefaultMK(color_ostream &out) +{ + return default_mk; +} +static size_t autobutcher_getDefaultMA(color_ostream &out) +{ + return default_ma; +} +static size_t autobutcher_getDefaultFK(color_ostream &out) +{ + return default_fk; +} +static size_t autobutcher_getDefaultFA(color_ostream &out) +{ + return default_fa; +} + +// set default target values for new races +static void autobutcher_setDefaultTargetNew(color_ostream &out, size_t fk, size_t mk, size_t fa, size_t ma) +{ + default_fk = fk; + default_mk = mk; + default_fa = fa; + default_ma = ma; + if(config_autobutcher.isValid()) + { + config_autobutcher.ival(3) = default_fk; + config_autobutcher.ival(4) = default_mk; + config_autobutcher.ival(5) = default_fa; + config_autobutcher.ival(6) = default_ma; + } +} + +// set default target values for ALL races (update watchlist and set new default) +static void autobutcher_setDefaultTargetAll(color_ostream &out, size_t fk, size_t mk, size_t fa, size_t ma) +{ + for(size_t i=0; ifk = fk; + w->mk = mk; + w->fa = fa; + w->ma = ma; + w->UpdateConfig(out); + } + autobutcher_setDefaultTargetNew(out, fk, mk, fa, ma); +} + +static std::string testString(color_ostream &out, size_t i) +{ + out << "where will this be written?" << std::endl; + return getRaceName(i); +} + +// DFHACK_LUA_FUNCTION(autobutcher_setEnabled), + +DFHACK_PLUGIN_LUA_FUNCTIONS { + DFHACK_LUA_FUNCTION(autobutcher_isEnabled), + DFHACK_LUA_FUNCTION(autowatch_isEnabled), + DFHACK_LUA_FUNCTION(autobutcher_setEnabled), + DFHACK_LUA_FUNCTION(autowatch_setEnabled), + DFHACK_LUA_FUNCTION(autobutcher_getSleep), + DFHACK_LUA_FUNCTION(autobutcher_setSleep), + DFHACK_LUA_FUNCTION(autobutcher_getWatchListSize), + DFHACK_LUA_FUNCTION(autobutcher_getWatchListRace), + + DFHACK_LUA_FUNCTION(autobutcher_isWatchListRaceWatched), + DFHACK_LUA_FUNCTION(autobutcher_setWatchListRaceWatched), + + DFHACK_LUA_FUNCTION(autobutcher_getWatchListRaceFK), + DFHACK_LUA_FUNCTION(autobutcher_getWatchListRaceFA), + DFHACK_LUA_FUNCTION(autobutcher_getWatchListRaceMK), + DFHACK_LUA_FUNCTION(autobutcher_getWatchListRaceMA), + + DFHACK_LUA_FUNCTION(autobutcher_setWatchListRaceFK), + DFHACK_LUA_FUNCTION(autobutcher_setWatchListRaceFA), + DFHACK_LUA_FUNCTION(autobutcher_setWatchListRaceMK), + DFHACK_LUA_FUNCTION(autobutcher_setWatchListRaceMA), + + DFHACK_LUA_FUNCTION(autobutcher_getDefaultFK), + DFHACK_LUA_FUNCTION(autobutcher_getDefaultFA), + DFHACK_LUA_FUNCTION(autobutcher_getDefaultMK), + DFHACK_LUA_FUNCTION(autobutcher_getDefaultMA), + + DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetNew), + DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetAll), + + DFHACK_LUA_FUNCTION(autobutcher_removeFromWatchList), + DFHACK_LUA_FUNCTION(autobutcher_sortWatchList), + + DFHACK_LUA_FUNCTION(testString), + DFHACK_LUA_END +}; + +// end lua API + + //START zone filters using df::global::ui_building_item_cursor; diff --git a/scripts/gui/autobutcher.lua b/scripts/gui/autobutcher.lua new file mode 100644 index 000000000..1c62775ba --- /dev/null +++ b/scripts/gui/autobutcher.lua @@ -0,0 +1,496 @@ +-- A GUI front-end for the autobutcher plugin. +-- requires to be called from the stock screen (z) + + +--[[ + + API overview (zone/autobutcher plugin functions which can be used by this lua script): + + autobutcher_isEnabled() - returns true if autobutcher is running + autowatch_isEnabled() - returns true if autowatch is running + autobutcher_getSleep() - get sleep timer in ticks + autobutcher_setSleep(int) - set sleep timer in ticks + autobutcher_getWatchListSize() - return size of watchlist + autobutcher_getWatchListRace(idx) - return race name for this watchlist index + + autobutcher_isWatchListRaceWatched(idx) - true if this watchlist index is watched + autobutcher_setWatchListRaceWatched(idx, bool) - set watchlist index to watched/unwatched + + autobutcher_getWatchListRaceFK() - get target fk + autobutcher_getWatchListRaceFA() - get target fa + autobutcher_getWatchListRaceMK() - get target mk + autobutcher_getWatchListRaceMA() - get target ma + + autobutcher_setWatchListRaceFK(value) - set fk + autobutcher_setWatchListRaceFA(value) - set fa + autobutcher_setWatchListRaceMK(value) - set mk + autobutcher_setWatchListRaceMA(value) - set ma + + autobutcher_removeFromWatchList(idx) - remove watchlist entry + autobutcher_sortWatchList() - sort the watchlist alphabetically + + testString(id) - returns race name for this race id (gonna be removed soon) + +--]] + + +local gui = require 'gui' +local utils = require 'utils' +local widgets = require 'gui.widgets' +local dlg = require 'gui.dialogs' + +local plugin = require 'plugins.zone' + +WatchList = defclass(WatchList, gui.FramedScreen) + +WatchList.ATTRS { + frame_title = 'Autobutcher Watchlist', + frame_inset = 0, -- cover full DF window + frame_background = COLOR_BLACK, + frame_style = gui.BOUNDARY_FRAME, +} + +-- width of the race name column in the UI +local racewidth = 25 + +function nextAutowatchState() + if(plugin.autowatch_isEnabled()) then + return 'stop' + end + return 'start' +end + +function nextAutobutcherState() + if(plugin.autobutcher_isEnabled()) then + return 'stop' + end + return 'start' +end + +function getSleepTimer() + return plugin.autobutcher_getSleep() +end + +function setSleepTimer(ticks) + plugin.autobutcher_setSleep(ticks) +end + +function WatchList:init(args) + self:addviews{ + widgets.Panel{ + frame = { l = 0, r = 0 }, + frame_inset = 1, + subviews = { + widgets.Label{ + frame = { l = 0, t = 0 }, + text_pen = COLOR_CYAN, + text = { + { text = 'Race', width = racewidth }, ' ', + { text = 'female', width = 6 }, ' ', + { text = ' male', width = 6 }, ' ', + { text = 'Female', width = 6 }, ' ', + { text = ' Male', width = 6 }, ' ', + { text = 'watching?' }, + NEWLINE, + { text = '', width = racewidth }, ' ', + { text = ' kids', width = 6 }, ' ', + { text = ' kids', width = 6 }, ' ', + { text = 'adults', width = 6 }, ' ', + { text = 'adults', width = 6 }, ' ', + } + }, + widgets.List{ + view_id = 'list', + frame = { t = 3, b = 3 }, + not_found_label = 'Watchlist is empty.', + edit_pen = COLOR_LIGHTCYAN, + text_pen = { fg = COLOR_GREY, bg = COLOR_BLACK }, + cursor_pen = { fg = COLOR_WHITE, bg = COLOR_GREEN }, + --on_select = self:callback('onSelectConstraint'), + }, + widgets.Label{ + view_id = 'bottom_ui', + frame = { b = 0, h = 1 }, + text = 'filled by updateBottom()' + } + } + }, + } + + self:initListChoices() + self:updateBottom() +end + +-- update the bottom part of the UI (after sleep timer changed etc) +function WatchList:updateBottom() + self.subviews.bottom_ui:setText( + { + { key = 'CUSTOM_F', text = ': f kids', + on_activate = self:callback('onEditFK') }, ', ', + { key = 'CUSTOM_M', text = ': m kids', + on_activate = self:callback('onEditMK') }, ', ', + { key = 'CUSTOM_SHIFT_F', text = ': f adults', + on_activate = self:callback('onEditFA') }, ', ', + { key = 'CUSTOM_SHIFT_M', text = ': m adults', + on_activate = self:callback('onEditMA') }, ', ', + { key = 'CUSTOM_SHIFT_X', text = ': Delete', + on_activate = self:callback('onDeleteConstraint') }, ', ', + { key = 'CUSTOM_W', text = ': toggle watch', + on_activate = self:callback('onToggleWatching') }, ', ', NEWLINE, + { key = 'CUSTOM_SHIFT_A', text = ': '..nextAutobutcherState()..' Autobutcher', + on_activate = self:callback('onToggleAutobutcher') }, ', ', + { key = 'CUSTOM_SHIFT_W', text = ': '..nextAutowatchState()..' Autowatch', + on_activate = self:callback('onToggleAutowatch') }, ', ', + { key = 'CUSTOM_SHIFT_S', text = ': sleep timer ('..getSleepTimer()..' ticks)', + on_activate = self:callback('onEditSleepTimer') }, ', ', + }) +end + +function WatchList:initListChoices() + + local choices = {} + + -- first two rows are for "edit all races" and "edit new races" + local fk = plugin.autobutcher_getDefaultFK() + local fa = plugin.autobutcher_getDefaultFA() + local mk = plugin.autobutcher_getDefaultMK() + local ma = plugin.autobutcher_getDefaultMA() + local watched = '---' + + table.insert (choices, { + text = { + { text = '!! ALL RACES PLUS NEW', width = racewidth, pad_char = ' ' }, --' ', + { text = tostring(fk), width = 7, rjustify = true }, + { text = tostring(mk), width = 7, rjustify = true }, + { text = tostring(fa), width = 7, rjustify = true }, + { text = tostring(ma), width = 7, rjustify = true }, + { text = watched, width = 6, rjustify = true } + } + }) + + table.insert (choices, { + text = { + { text = '!! ONLY NEW RACES', width = racewidth, pad_char = ' ' }, --' ', + { text = tostring(fk), width = 7, rjustify = true }, + { text = tostring(mk), width = 7, rjustify = true }, + { text = tostring(fa), width = 7, rjustify = true }, + { text = tostring(ma), width = 7, rjustify = true }, + { text = watched, width = 6, rjustify = true } + } + }) + + -- fill with watchlist + for i=0, plugin.autobutcher_getWatchListSize()-1 do + local racestr = plugin.autobutcher_getWatchListRace(i) + fk = plugin.autobutcher_getWatchListRaceFK(i) + fa = plugin.autobutcher_getWatchListRaceFA(i) + mk = plugin.autobutcher_getWatchListRaceMK(i) + ma = plugin.autobutcher_getWatchListRaceMA(i) + local watched = 'no' + if plugin.autobutcher_isWatchListRaceWatched(i) then + watched = 'yes' + end + + table.insert (choices, { + text = { + { text = racestr, width = racewidth, pad_char = ' ' }, --' ', + { text = tostring(fk), width = 7, rjustify = true }, + { text = tostring(mk), width = 7, rjustify = true }, + { text = tostring(fa), width = 7, rjustify = true }, + { text = tostring(ma), width = 7, rjustify = true }, + { text = watched, width = 6, rjustify = true } + } + }) + end + + local list = self.subviews.list + list:setChoices(choices) +end + +function WatchList:onInput(keys) + if keys.LEAVESCREEN then + self:dismiss() + else + WatchList.super.onInput(self, keys) + end +end + +-- check the user input for target population values +function WatchList:checkUserInput(count, text) + if count == nil then + dlg.showMessage('Invalid Number', 'This is not a number: '..text..NEWLINE..'(for zero enter a 0)', COLOR_LIGHTRED) + return false + end + if count < 0 then + dlg.showMessage('Invalid Number', 'Negative numbers make no sense!', COLOR_LIGHTRED) + return false + end + return true +end + +-- check the user input for sleep timer +function WatchList:checkUserInputSleep(count, text) + if count == nil then + dlg.showMessage('Invalid Number', 'This is not a number: '..text..NEWLINE..'(for zero enter a 0)', COLOR_LIGHTRED) + return false + end + if count < 1000 then + dlg.showMessage('Invalid Number', + 'Minimum allowed timer value is 1000!'..NEWLINE..'Too low values could decrease performance'..NEWLINE..'and are not necessary!', + COLOR_LIGHTRED) + return false + end + return true +end + +function WatchList:onEditFK() + local selidx,selobj = self.subviews.list:getSelected() + local fk = plugin.autobutcher_getDefaultFK(); + local mk = plugin.autobutcher_getDefaultMK(); + local fa = plugin.autobutcher_getDefaultFA(); + local ma = plugin.autobutcher_getDefaultMA(); + local race = 'ALL RACES PLUS NEW' + + if selidx == 2 then + race = 'ONLY NEW RACES' + end + + local watchindex = selidx - 3 + if selidx > 2 then + fk = plugin.autobutcher_getWatchListRaceFK(watchindex) + race = plugin.autobutcher_getWatchListRace(watchindex) + end + + dlg.showInputPrompt( + 'Race: '..race, + 'Enter desired maximum of female kids:', + COLOR_WHITE, + ' '..fk, + function(text) + local count = tonumber(text) + if self:checkUserInput(count, text) then + fk = count + if selidx == 1 then + plugin.autobutcher_setDefaultTargetAll( fk, mk, fa, ma ) + end + if selidx == 2 then + plugin.autobutcher_setDefaultTargetNew( fk, mk, fa, ma ) + end + if selidx > 2 then + plugin.autobutcher_setWatchListRaceFK(watchindex, fk) + end + self:initListChoices() + end + end + ) +end + +function WatchList:onEditMK() + local selidx = self.subviews.list:getSelected() + local fk = plugin.autobutcher_getDefaultFK(); + local mk = plugin.autobutcher_getDefaultMK(); + local fa = plugin.autobutcher_getDefaultFA(); + local ma = plugin.autobutcher_getDefaultMA(); + local race = 'ALL RACES PLUS NEW' + + if selidx == 2 then + race = 'ONLY NEW RACES' + end + + local watchindex = selidx - 3 + if selidx > 2 then + mk = plugin.autobutcher_getWatchListRaceMK(watchindex) + race = plugin.autobutcher_getWatchListRace(watchindex) + end + + dlg.showInputPrompt( + 'Race: '..race, + 'Enter desired maximum of male kids:', + COLOR_WHITE, + ' '..mk, + function(text) + local count = tonumber(text) + if self:checkUserInput(count, text) then + mk = count + if selidx == 1 then + plugin.autobutcher_setDefaultTargetAll( fk, mk, fa, ma ) + end + if selidx == 2 then + plugin.autobutcher_setDefaultTargetNew( fk, mk, fa, ma ) + end + if selidx > 2 then + plugin.autobutcher_setWatchListRaceMK(watchindex, mk) + end + self:initListChoices() + end + end + ) +end + +function WatchList:onEditFA() + local selidx = self.subviews.list:getSelected() + local fk = plugin.autobutcher_getDefaultFK(); + local mk = plugin.autobutcher_getDefaultMK(); + local fa = plugin.autobutcher_getDefaultFA(); + local ma = plugin.autobutcher_getDefaultMA(); + local race = 'ALL RACES PLUS NEW' + + if selidx == 2 then + race = 'ONLY NEW RACES' + end + + local watchindex = selidx - 3 + if selidx > 2 then + fa = plugin.autobutcher_getWatchListRaceFA(watchindex) + race = plugin.autobutcher_getWatchListRace(watchindex) + end + + dlg.showInputPrompt( + 'Race: '..race, + 'Enter desired maximum of female adults:', + COLOR_WHITE, + ' '..fa, + function(text) + local count = tonumber(text) + if self:checkUserInput(count, text) then + fa = count + if selidx == 1 then + plugin.autobutcher_setDefaultTargetAll( fk, mk, fa, ma ) + end + if selidx == 2 then + plugin.autobutcher_setDefaultTargetNew( fk, mk, fa, ma ) + end + if selidx > 2 then + plugin.autobutcher_setWatchListRaceFA(watchindex, fa) + end + self:initListChoices() + end + end + ) +end + +function WatchList:onEditMA() + local selidx = self.subviews.list:getSelected() + local fk = plugin.autobutcher_getDefaultFK(); + local mk = plugin.autobutcher_getDefaultMK(); + local fa = plugin.autobutcher_getDefaultFA(); + local ma = plugin.autobutcher_getDefaultMA(); + local race = 'ALL RACES PLUS NEW' + + if selidx == 2 then + race = 'ONLY NEW RACES' + end + + local watchindex = selidx - 3 + if selidx > 2 then + ma = plugin.autobutcher_getWatchListRaceMA(watchindex) + race = plugin.autobutcher_getWatchListRace(watchindex) + end + + dlg.showInputPrompt( + 'Race: '..race, + 'Enter desired maximum of male adults:', + COLOR_WHITE, + ' '..ma, + function(text) + local count = tonumber(text) + if self:checkUserInput(count, text) then + ma = count + if selidx == 1 then + plugin.autobutcher_setDefaultTargetAll( fk, mk, fa, ma ) + end + if selidx == 2 then + plugin.autobutcher_setDefaultTargetNew( fk, mk, fa, ma ) + end + if selidx > 2 then + plugin.autobutcher_setWatchListRaceMA(watchindex, ma) + end + self:initListChoices() + end + end + ) +end + +function WatchList:onEditSleepTimer() + local sleep = getSleepTimer() + dlg.showInputPrompt( + 'Edit Sleep Timer', + 'Enter new sleep timer in ticks:'..NEWLINE..'(1 ingame day equals 1200 ticks)', + COLOR_WHITE, + ' '..sleep, + function(text) + local count = tonumber(text) + if self:checkUserInputSleep(count, text) then + sleep = count + setSleepTimer(sleep) + self:updateBottom() + end + end + ) +end + +function WatchList:onToggleWatching() + local selidx = self.subviews.list:getSelected() + if(selidx == 0) then + --print('special handling for zero - list empty?') + end + if (selidx == 1) then + --print('special handling for first row - ALL animals') + end + if (selidx == 2) then + --print('special handling for second row - NEW animals') + end + if selidx > 2 then + --print('handling for single animal on watchlist') + local idx = selidx - 3 + if plugin.autobutcher_isWatchListRaceWatched(idx) then + plugin.autobutcher_setWatchListRaceWatched(idx, false) + else + plugin.autobutcher_setWatchListRaceWatched(idx, true) + end + end + self:initListChoices() +end + +function WatchList:onDeleteConstraint() + local selidx,selobj = self.subviews.list:getSelected() + if(selidx < 3) then + -- print('cannot delete this entry') + return + end + local idx = selidx - 3 + dlg.showYesNoPrompt( + 'Delete from Watchlist', + 'Really delete the selected entry?'..NEWLINE..'(you could just toggle watch instead)', + COLOR_YELLOW, + function() + plugin.autobutcher_removeFromWatchList(idx) + self:initListChoices() + end + ) +end + +function WatchList:onToggleAutobutcher() + if(plugin.autobutcher_isEnabled()) then + plugin.autobutcher_setEnabled(false) + plugin.autobutcher_sortWatchList() + else + plugin.autobutcher_setEnabled(true) + end + self:initListChoices() + self:updateBottom() +end + +function WatchList:onToggleAutowatch() + if(plugin.autowatch_isEnabled()) then + plugin.autowatch_setEnabled(false) + else + plugin.autowatch_setEnabled(true) + end + self:initListChoices() + self:updateBottom() +end + + +local screen = WatchList{ } +screen:show()