From 6868cab1fe4aeb15da435956d14c769e4d4e8058 Mon Sep 17 00:00:00 2001 From: Warmist Date: Sun, 3 Aug 2014 15:11:38 +0300 Subject: [PATCH] Exposed dfhack.jobs.linkIntoWorld. Simplified eventful and gui.dwarfmode for sidebar use/creation. --- library/LuaApi.cpp | 1 + library/lua/gui/dwarfmode.lua | 29 +++++++++++++++++++++++++++-- plugins/building-hacks.cpp | 15 +++++++++++++++ plugins/lua/building-hacks.lua | 4 ++-- plugins/lua/eventful.lua | 21 +++++++++++++++++---- scripts/gui/mod-manager.lua | 18 +++++++++--------- 6 files changed, 71 insertions(+), 17 deletions(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 53f4b08e6..3fd18a8d6 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1339,6 +1339,7 @@ static const LuaWrapper::FunctionReg dfhack_job_module[] = { WRAPM(Job,isSuitableItem), WRAPM(Job,isSuitableMaterial), WRAPM(Job,getName), + WRAPM(Job,linkIntoWorld), WRAPN(is_equal, jobEqual), WRAPN(is_item_equal, jobItemEqual), { NULL, NULL } diff --git a/library/lua/gui/dwarfmode.lua b/library/lua/gui/dwarfmode.lua index 8c537639b..a008bb135 100644 --- a/library/lua/gui/dwarfmode.lua +++ b/library/lua/gui/dwarfmode.lua @@ -383,7 +383,7 @@ MenuOverlay = defclass(MenuOverlay, DwarfOverlay) MenuOverlay.ATTRS { frame_inset = 0, - frame_background = CLEAR_PEN, + frame_background = gui.CLEAR_PEN, } function MenuOverlay:computeFrame(parent_rect) @@ -417,5 +417,30 @@ function MenuOverlay:render(dc) MenuOverlay.super.render(self, dc) end end - +--fakes a "real" workshop sidebar menu, but on exactly selected workshop +WorkshopOverlay = defclass(WorkshopOverlay, MenuOverlay) +WorkshopOverlay.ATTRS={ + workshop=DEFAULT_NIL, +} +function WorkshopOverlay:onInput(keys) + local allowedKeys={ --TODO add options: job management, profile, etc... + "CURSOR_RIGHT","CURSOR_LEFT","CURSOR_UP","CURSOR_DOWN", + "CURSOR_UPRIGHT","CURSOR_UPLEFT","CURSOR_DOWNRIGHT","CURSOR_DOWNLEFT","CURSOR_UP_Z","CURSOR_DOWN_Z","DESTROYBUILDING"} + if keys.LEAVESCREEN then + self:dismiss() + self:sendInputToParent('LEAVESCREEN') + else + for _,name in ipairs(allowedKeys) do + if keys[name] then + self:sendInputToParent(name) + break + end + end + self:inputToSubviews(keys) + end + if df.global.world.selected_building ~= self.workshop then + self:dismiss() + return + end +end return _ENV diff --git a/plugins/building-hacks.cpp b/plugins/building-hacks.cpp index 07beade74..d56215fda 100644 --- a/plugins/building-hacks.cpp +++ b/plugins/building-hacks.cpp @@ -47,6 +47,7 @@ struct workshop_hack_data int frame_skip; // e.g. 2 means have to ticks between frames //updateCallback: int skip_updates; + int room_subset; //0 no, 1 yes, -1 default }; typedef std::map workshops_data_t; workshops_data_t hacked_workshops; @@ -174,6 +175,17 @@ struct work_hook : df::building_workshopst{ return INTERPOSE_NEXT(isUnpowered)(); } + DEFINE_VMETHOD_INTERPOSE(bool, canBeRoomSubset, ()) + { + if(auto def = find_def()) + { + if(def->room_subset==0) + return false; + if(def->room_subset==1) + return true; + } + return INTERPOSE_NEXT(canBeRoomSubset)(); + } DEFINE_VMETHOD_INTERPOSE(void, updateAction, ()) { if(auto def = find_def()) @@ -241,6 +253,7 @@ IMPLEMENT_VMETHOD_INTERPOSE(work_hook, categorize); IMPLEMENT_VMETHOD_INTERPOSE(work_hook, uncategorize); IMPLEMENT_VMETHOD_INTERPOSE(work_hook, canConnectToMachine); IMPLEMENT_VMETHOD_INTERPOSE(work_hook, isUnpowered); +IMPLEMENT_VMETHOD_INTERPOSE(work_hook, canBeRoomSubset); IMPLEMENT_VMETHOD_INTERPOSE(work_hook, updateAction); IMPLEMENT_VMETHOD_INTERPOSE(work_hook, drawBuilding); void clear_mapping() @@ -336,6 +349,7 @@ static int addBuilding(lua_State* L) else newDefinition.machine_timing=false; } + newDefinition.room_subset=luaL_optinteger(L,9,-1); hacked_workshops[newDefinition.myType]=newDefinition; return 0; } @@ -354,6 +368,7 @@ static void enable_hooks(bool enable) INTERPOSE_HOOK(work_hook,uncategorize).apply(enable); INTERPOSE_HOOK(work_hook,canConnectToMachine).apply(enable); INTERPOSE_HOOK(work_hook,isUnpowered).apply(enable); + INTERPOSE_HOOK(work_hook,canBeRoomSubset).apply(enable); //update n render INTERPOSE_HOOK(work_hook,updateAction).apply(enable); INTERPOSE_HOOK(work_hook,drawBuilding).apply(enable); diff --git a/plugins/lua/building-hacks.lua b/plugins/lua/building-hacks.lua index e0a5b1010..033a01d5d 100644 --- a/plugins/lua/building-hacks.lua +++ b/plugins/lua/building-hacks.lua @@ -102,8 +102,8 @@ function registerBuilding(args) end frames=processFrames(shop_def,animate.frames) end - - addBuilding(shop_id,fix_impassible,consume,produce,gears,updateSkip,frames,frameLength) + local roomSubset=args.canBeRoomSubset or -1 + addBuilding(shop_id,fix_impassible,consume,produce,gears,updateSkip,frames,frameLength,roomSubset) end return _ENV \ No newline at end of file diff --git a/plugins/lua/eventful.lua b/plugins/lua/eventful.lua index 6e41271ad..91c173e75 100644 --- a/plugins/lua/eventful.lua +++ b/plugins/lua/eventful.lua @@ -98,10 +98,23 @@ function registerReaction(reaction_name,callback) end function registerSidebar(shop_name,callback) - _registeredStuff.customSidebar=_registeredStuff.customSidebar or {} - _registeredStuff.customSidebar[shop_name]=callback - onWorkshopFillSidebarMenu._library=customSidebarsCallback - dfhack.onStateChange.eventful=unregall + if type(callback)=="function" then + _registeredStuff.customSidebar=_registeredStuff.customSidebar or {} + _registeredStuff.customSidebar[shop_name]=callback + onWorkshopFillSidebarMenu._library=customSidebarsCallback + dfhack.onStateChange.eventful=unregall + else + local function drawSidebar( wshop ) + local valid_focus="dwarfmode/QueryBuilding/Some" + if string.sub(dfhack.gui.getCurFocus(),1,#valid_focus)==valid_focus and + wshop:getMaxBuildStage()==wshop:getBuildStage() + then + local sidebar=callback{workshop=wshop} + sidebar:show() + end + end + registerSidebar(shop_name,drawSidebar) + end end function removeNative(shop_name,name) diff --git a/scripts/gui/mod-manager.lua b/scripts/gui/mod-manager.lua index 210109807..a4f503868 100644 --- a/scripts/gui/mod-manager.lua +++ b/scripts/gui/mod-manager.lua @@ -34,7 +34,7 @@ function fileExists(filename) end end if not fileExists(init_file) then - local initFile=io.open(initFileName,"a") + local initFile=io.open(init_file,"a") initFile:close() end function copyFile(from,to) --oh so primitive @@ -52,15 +52,15 @@ function patchInit(initFileName,patch_guard,code) code,patch_guard[2])) initFile:close() end -function patchDofile( initFileName,patch_guard,dofile_list ) - local initFile=io.open(initFileName,"a") - initFile:write(patch_guard[1].."\n") +function patchDofile( luaFileName,patch_guard,dofile_list,mod_path ) + local luaFile=io.open(luaFileName,"a") + luaFile:write(patch_guard[1].."\n") for _,v in ipairs(dofile_list) do - local fixed_path=mod_dir:gsub("\\","/") - initFile:write(string.format("dofile('%s/%s')\n",fixed_path,v)) + local fixed_path=mod_path:gsub("\\","/") + luaFile:write(string.format("dofile('%s/%s')\n",fixed_path,v)) end - initFile:write(patch_guard[2].."\n") - initFile:close() + luaFile:write(patch_guard[2].."\n") + luaFile:close() end function patchFile(file_name,patch_guard,after_string,code) local input_lines=patch_guard[1].."\n"..code.."\n"..patch_guard[2] @@ -294,7 +294,7 @@ function manager:install(trgMod,force) patchInit(init_file,trgMod.guard_init,trgMod.patch_init) end if trgMod.patch_dofile then - patchDofile(init_file,trgMod.guard_init,trgMod.patch_dofile) + patchDofile(init_file,trgMod.guard_init,trgMod.patch_dofile,trgMod.path) end trgMod.installed=true