diff options
Diffstat (limited to 'signs_api/init.lua')
| -rw-r--r-- | signs_api/init.lua | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/signs_api/init.lua b/signs_api/init.lua new file mode 100644 index 0000000..9fcaa10 --- /dev/null +++ b/signs_api/init.lua @@ -0,0 +1,218 @@ +--[[ + signs mod for Minetest - Various signs with text displayed on + (c) Pierre-Yves Rollo + + This file is part of signs. + + signs is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + signs is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with signs. If not, see <http://www.gnu.org/licenses/>. +--]] + +signs_api = {} +signs_api.name = minetest.get_current_modname() +signs_api.path = minetest.get_modpath(signs_api.name) + +-- Load support for intllib. +local S, NS = dofile(signs_api.path.."/intllib.lua") +signs_api.intllib = S +local F = function(...) return minetest.formspec_escape(S(...)) end + +function signs_api.set_display_text(pos, text, font) + local meta = minetest.get_meta(pos) + meta:set_string("display_text", text) + if text and text ~= "" then + meta:set_string("infotext", "\""..text.."\"") + else + meta:set_string("infotext", "") + end + if font then + meta:set_string("font", font) + end + display_api.update_entities(pos) +end + +function signs_api.set_formspec(pos) + local meta = minetest.get_meta(pos) + local ndef = minetest.registered_nodes[minetest.get_node(pos).name] + if ndef and ndef.display_entities + and ndef.display_entities["signs:display_text"] then + local maxlines = ndef.display_entities["signs:display_text"].maxlines + local fs, y + + if maxlines == 1 then + fs = "field[0.5,0.7;5.5,1;display_text;"..F("Text").. + ";${display_text}]" + y = 1.2 + else + local extralabel = "" + if maxlines then + extralabel = F(" (first %s lines only)"):format(maxlines) + end + + fs = "textarea[0.5,0.7;5.5,2;display_text;"..F("Text").."".. + extralabel..";${display_text}]" + y = 2.4 + end + + fs = fs.."button[1,"..y..";2,1;font;"..F("Font").."]" + fs = fs.."button_exit[3,"..y..";2,1;ok;"..F("Write").."]" + y = y + 0.8 + fs = "size[6,"..y.."]"..default.gui_bg.. + default.gui_bg_img..default.gui_slots..fs + + meta:set_string("formspec", fs) + end +end + +function signs_api.on_receive_fields(pos, formname, fields, player) + if not minetest.is_protected(pos, player:get_player_name()) then + if fields and (fields.ok or fields.key_enter) then + signs_api.set_display_text(pos, fields.display_text) + end + if fields and (fields.font) then + signs_api.set_display_text(pos, fields.display_text) + font_api.show_font_list(player, pos) + end + end +end + +-- On place callback for direction signs +-- (chooses which sign according to look direction) +function signs_api.on_place_direction(itemstack, placer, pointed_thing) + local name = itemstack:get_name() + local ndef = minetest.registered_nodes[name] + + local bdir = {x = pointed_thing.under.x - pointed_thing.above.x, + y = pointed_thing.under.y - pointed_thing.above.y, + z = pointed_thing.under.z - pointed_thing.above.z} + local pdir = placer:get_look_dir() + + local ndir, test + + if ndef.paramtype2 == "facedir" then + if bdir.x == 0 and bdir.z == 0 then + -- Ceiling or floor pointed (facedir chosen from player dir) + ndir = minetest.dir_to_facedir({x=pdir.x, y=0, z=pdir.z}) + else + -- Wall pointed + ndir = minetest.dir_to_facedir(bdir) + end + + test = {[0]=-pdir.x, pdir.z, pdir.x, -pdir.z} + end + + if ndef.paramtype2 == "wallmounted" then + ndir = minetest.dir_to_wallmounted(bdir) + if ndir == 0 or ndir == 1 then + -- Ceiling or floor + ndir = minetest.dir_to_wallmounted({x=pdir.x, y=0, z=pdir.z}) + end + + test = {0, pdir.z, -pdir.z, -pdir.x, pdir.x} + end + + -- Only for direction signs + if ndef.signs_other_dir then + if test[ndir] > 0 then + itemstack:set_name(ndef.signs_other_dir) + end + itemstack = minetest.item_place(itemstack, placer, pointed_thing, ndir) + itemstack:set_name(name) + + return itemstack + else + return minetest.item_place(itemstack, placer, pointed_thing, ndir) + end +end + +-- Handles screwdriver rotation. Direction is affected for direction signs +-- If rotation mode is 2 and sign is directional, swap direction. +-- Otherwise use display_api's on_rotate function. +function signs_api.on_rotate(pos, node, player, mode, new_param2) + if mode == 2 then + local ndef = minetest.registered_nodes[node.name]
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef UTIL_THREAD_HEADER
#define UTIL_THREAD_HEADER
#include "../irrlichttypes.h"
#include "../threading/thread.h"
#include "../threading/mutex.h"
#include "../threading/mutex_auto_lock.h"
#include "porting.h"
#include "log.h"
template<typename T>
class MutexedVariable {
public:
MutexedVariable(T value):
m_value(value)
{}
T get()
{
MutexAutoLock lock(m_mutex);
return m_value;
}
void set(T value)
{
MutexAutoLock lock(m_mutex);
m_value = value;
}
// You'll want to grab this in a SharedPtr
MutexAutoLock *getLock()
{
return new MutexAutoLock(m_mutex);
}
// You pretty surely want to grab the lock when accessing this
T m_value;
private:
Mutex m_mutex;
};
/*
A single worker thread - multiple client threads queue framework.
*/
template<typename Key, typename T, typename Caller, typename CallerData>
class GetResult {
public:
Key key;
T item;
std::pair<Caller, CallerData> caller;
};
template<typename Key, typename T, typename Caller, typename CallerData>
class ResultQueue : public MutexedQueue<GetResult<Key, T, Caller, CallerData> > {
};
template<typename Caller, typename Data, typename Key, typename T>
class CallerInfo {
public:
Caller caller;
Data data;
ResultQueue<Key, T, Caller, Data> *dest;
};
template<typename Key, typename T, typename Caller, typename CallerData>
class GetRequest {
public:
GetRequest() {}
~GetRequest() {}
GetRequest(Key a_key) {
key = a_key;
}
Key key;
std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
};
/**
* Notes for RequestQueue usage
* @param Key unique key to identify a request for a specific resource
* @param T ?
* @param Caller unique id of calling thread
* @param CallerData data passed back to caller
*/
template<typename Key, typename T, typename Caller, typename CallerData>
class RequestQueue {
public:
bool empty()
{
return m_queue.empty();
}
void add(Key key, Caller caller, CallerData callerdata,
ResultQueue<Key, T, Caller, CallerData> *dest)
{
typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
{
MutexAutoLock lock(m_queue.getMutex());
/*
If the caller is already on the list, only update CallerData
*/
for (i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) {
GetRequest<Key, T, Caller, CallerData> &request = *i;
if (request.key != key)
continue;
for (j = request.callers.begin(); j != request.callers.end(); ++j) {
CallerInfo<Caller, CallerData, Key, T> &ca = *j;
if (ca.caller == caller) {
ca.data = callerdata;
return;
}
}
CallerInfo<Caller, CallerData, Key, T> ca;
ca.caller = caller;
ca.data = callerdata;
ca.dest = dest;
request.callers.push_back(ca);
return;
}
}
/*
Else add a new request to the queue
*/
GetRequest<Key, T, Caller, CallerData> request;
request.key = key;
CallerInfo<Caller, CallerData, Key, T> ca;
ca.caller = caller;
ca.data = callerdata;
ca.dest = dest;
request.callers.push_back(ca);
m_queue.push_back(request);
}
GetRequest<Key, T, Caller, CallerData> pop(unsigned int timeout_ms)
{
return m_queue.pop_front(timeout_ms);
}
GetRequest<Key, T, Caller, CallerData> pop()
{
return m_queue.pop_frontNoEx();
}
void pushResult(GetRequest<Key, T, Caller, CallerData> req, T res)
{
for (typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator
i = req.callers.begin();
i != req.callers.end(); ++i) {
CallerInfo<Caller, CallerData, Key, T> &ca = *i;
GetResult<Key,T,Caller,CallerData> result;
result.key = req.key;
result.item = res;
result.caller.first = ca.caller;
result.caller.second = ca.data;
ca.dest->push_back(result);
}
}
private:
MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
};
class UpdateThread : public Thread
{
public:
UpdateThread(const std::string &name) : Thread(name + "Update") {}
~UpdateThread() {}
void deferUpdate() { m_update_sem.post(); }
void stop()
{
Thread::stop();
// give us a nudge
m_update_sem.post();
}
void *run()
{
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
while (!stopRequested()) {
m_update_sem.wait();
// Set semaphore to 0
while (m_update_sem.wait(0));
if (stopRequested()) break;
doUpdate();
}
END_DEBUG_EXCEPTION_HANDLER(errorstream)
return NULL;
}
protected:
virtual void doUpdate() = 0;
private:
Semaphore m_update_sem;
};
#endif
|
