Java Code Examples for org.luaj.vm2.LuaValue#istable()

The following examples show how to use org.luaj.vm2.LuaValue#istable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VideoOSLuaView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public void callLuaFunction(String functionName, HashMap<String, String> map) {
    if (mLuaView != null) {
        LuaValue dataTable = null;
        String key = "data";
        Object dataValue = map.get(key);
        if (dataValue != null && dataValue instanceof String) {
            dataTable = JsonUtil.toLuaTable((String) dataValue);
            if (dataTable != null && dataTable.istable()) {
                map.remove(key);
            }
        }
        LuaValue table = LuaUtil.toTable(map);
        if (dataTable != null && table != null && table.istable()) {
            table.set(key, dataTable);
        }
        mLuaView.callLuaFunction(functionName, table);
    }
}
 
Example 2
Source File: Utilities.java    From Lukkit with MIT License 6 votes vote down vote up
/**
 * Gets the Java object from a LuaValue.
 *
 * @param value the LuaValue
 * @return the Java object
 */
public static Object getObjectFromLuavalue(LuaValue value) {
    if (value.istable()) {
        return convertTable(value.checktable());
    } else if (value.isint()) {
        return value.checkint();
    } else if (value.islong()) {
        return value.checklong();
    } else if (value.isnumber()) {
        return value.checkdouble();
    } else if (value.isstring()) {
        return value.checkjstring();
    } else if (value.isboolean()) {
        return value.checkboolean();
    } else if (value.isnil()) {
        return null;
    } else {
        return value.checkuserdata();
    }
}
 
Example 3
Source File: UDBaseListOrRecyclerView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 调用 Callback 方法
 *
 * @param cell
 * @param position
 * @return
 */
public LuaValue onCellClicked(LuaValue cell, int position) {
    int section = getSectionByPosition(position);
    int row = getRowInSectionByPosition(position);
    LuaValue cellData = getCell(getId(position, section, row));
    if (cellData != null) {
         LuaValue callback = cellData.get("Callback");
        if (callback.isfunction()) {
            return LuaUtil.callFunction(callback, cell, LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
        } else if (callback.istable()) {
            return LuaUtil.callFunction(LuaUtil.getFunction(callback, "Click", "click"), cell, LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
        }
    }
    return NIL;
}
 
Example 4
Source File: UDBaseListOrRecyclerView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 调用 Callback 方法
 *
 * @param cell
 * @param position
 * @return
 */
public boolean onCellLongClicked(LuaValue cell, int position) {
     int section = getSectionByPosition(position);
     int row = getRowInSectionByPosition(position);
     LuaValue cellData = getCell(getId(position, section, row));
    if (cellData != null) {
         LuaValue callback = cellData.get("Callback");
        if (callback != null && callback.istable()) {
            return LuaUtil.callFunction(LuaUtil.getFunction(callback, "LongClick", "longClick"), cell, LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row)).optboolean(false);
        }
    }
    return false;
}
 
Example 5
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert a table to map
 *
 * @param table
 * @return
 */
public static HashMap<String, String> toMap(LuaTable table) {
    if (table != null) {
        final HashMap<String, String> result = new HashMap<String, String>();
        final LuaValue[] keys = table.keys();
        for (LuaValue key : keys) {
            LuaValue luaValue = table.get(key);
            String value = null;
            if (luaValue.istable()) {
                value = JsonUtil.toString(luaValue);
            } else {
                if (luaValue instanceof LuaBoolean) {
                    value = String.valueOf(luaValue.optboolean(false));
                } else if (luaValue instanceof LuaInteger) {
                    value = String.valueOf(luaValue.optint(0));
                } else if (luaValue instanceof LuaDouble) {
                    value = String.valueOf(luaValue.optdouble(0));
                } else if (luaValue instanceof LuaString) {
                    value = String.valueOf(luaValue.optstring(null));
                } else {
                    value = String.valueOf(luaValue);
                }
            }
            if (value != null) {
                result.put(key.optjstring(null), value);
            }
        }
        return result;
    }
    return null;
}
 
Example 6
Source File: SampleSandboxed.java    From luaj with MIT License 5 votes vote down vote up
public ReadOnlyLuaTable(LuaValue table) {
	presize(table.length(), 0);
	for (Varargs n = table.next(LuaValue.NIL); !n.arg1().isnil(); n = table
			.next(n.arg1())) {
		LuaValue key = n.arg1();
		LuaValue value = n.arg(2);
		super.rawset(key, value.istable() ? new ReadOnlyLuaTable(value) : value);
	}
}
 
Example 7
Source File: LuaEngine.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static LuaTable moduleInstance(String module) {
	LuaValue luaModule = getEngine().call("dofile", module+".lua");

	if(luaModule.istable()) {
		return luaModule.checktable();
	}

	EventCollector.logException("failed to load instance of lua module: "+module);
	return null;
}
 
Example 8
Source File: ReadOnlyLuaTable.java    From Cubes with MIT License 5 votes vote down vote up
public ReadOnlyLuaTable(LuaValue table) {
  presize(table.length(), 0);
  for (Varargs n = table.next(LuaValue.NIL); !n.arg1().isnil(); n = table
          .next(n.arg1())) {
    LuaValue key = n.arg1();
    LuaValue value = n.arg(2);
    super.rawset(key, value.istable() ? new ReadOnlyLuaTable(value) : value);
  }
}
 
Example 9
Source File: UDSpannableString.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
private void initSpannableStringBuilder(LuaValue text, LuaValue config) {
    SpannableStringBuilder spannableStringBuilder = getSpannableStringBuilder();
    if (text != null && text.isstring()) {
        spannableStringBuilder = spannableStringBuilder.append(text.tojstring());
    }

    if (spannableStringBuilder.length() > 0) {
        if (config != null && config.istable()) {
             int end = spannableStringBuilder.length();
             int fontSize = DimenUtil.spToPx(config.get("fontSize").optint(-1));
             Integer fontColor = ColorUtil.parse(LuaUtil.getValue(config, "fontColor"));
             String fontName = config.get("fontName").optjstring(null);

             LuaValue weightValue = config.get("fontWeight");
             int fontWeight = LuaUtil.isNumber(weightValue) ? weightValue.optint(UDFontWeight.WEIGHT_NORMAL_INT) : UDFontWeight.getValue(weightValue.optjstring(UDFontWeight.WEIGHT_NORMAL));

             LuaValue styleValue = config.get("fontStyle");
             int fontStyle = LuaUtil.isNumber(styleValue) ? styleValue.optint(Typeface.NORMAL) : UDFontStyle.getValue(styleValue.optjstring(UDFontStyle.STYLE_NORMAL));

             Integer backgroundColor = ColorUtil.parse(LuaUtil.getValue(config, "backgroundColor"));
             boolean strikethrough = config.get("strikethrough").optboolean(false);
             boolean underline = config.get("underline").optboolean(false);

            if (fontSize != -1) {//字体大小
                spannableStringBuilder.setSpan(new AbsoluteSizeSpan(fontSize), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontColor != null) {//字体颜色
                spannableStringBuilder.setSpan(new ForegroundColorSpan(fontColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontName != null && getLuaResourceFinder() != null) {//字体
                spannableStringBuilder.setSpan(new CustomTypefaceSpan(fontName, getLuaResourceFinder().findTypeface(fontName)), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontWeight != -1 && fontWeight > UDFontWeight.WEIGHT_NORMAL_INT) {//文字Weight
                spannableStringBuilder.setSpan(new WeightStyleSpan(fontWeight), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontStyle != -1 && (fontStyle >= Typeface.NORMAL && fontStyle <= Typeface.BOLD_ITALIC)) {//文字样式
                spannableStringBuilder.setSpan(new StyleSpan(fontStyle), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (backgroundColor != null) {//背景色
                spannableStringBuilder.setSpan(new BackgroundColorSpan(backgroundColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (strikethrough) {//删除线
                spannableStringBuilder.setSpan(new StrikethroughSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (underline) {//下划线
                spannableStringBuilder.setSpan(new UnderlineSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}