simple color assignment not working in XTK with GWT (mesh.color = [0.7,0,0]) - xtk

#haehn Hi Haehn (XTK)
I'm using edge-XTK with GWT and trying to render a simple STL. However XTK code fails at the line where we assign color to the mesh.
mesh.color = [0.7,0,0] // this line fails
Error message emitted by XTK code: "Invalid color"
This behavior is observed only when using XTK with GWT.
The error seems to be coming from this XTK code snippet
X.displayable.prototype.__defineSetter__('color', function(color) {
// we accept only numbers as arguments
if (!goog.isDefAndNotNull(color) || !(color instanceof Array) ||
(color.length != 3)) {
throw new Error('Invalid color.');
}
I'm guessing that the issue is with the way GWT builds page with iframes... because of which the above if condition could be failing in GWT. I think if you replace the above check with following snippet (got idea from: here).
It might fix the problem.
use goog.isArray(color) instead of (color instanceof Array)
Can you please investigate and comment?
Edit:
Hi XTK
Here is the code snippet which shows how I'm using XTK with GWT.
public class TestGwtXtk implements EntryPoint {
public void onModuleLoad() {
testXtk();
}
// GWT JSNI method, which allows mixing Java and JS natively.
// it is akin using c++ or c libraries in java or android
private native void testXtk() /*-{
var r = new $wnd.X.renderer3D();
r.container = 'xtk_container'; // div ele
r.config.PROGRESSBAR_ENABLED = false;
r.init();
cube = new $wnd.X.cube();
cube.lengthX = cube.lengthY = cube.lengthZ = 20;
cube.color = [ 1, 1, 1 ]; // fails here in XTK code
cube.center = [ 0, 0, 0 ]; // fails here in XTK code
r.add(cube);
r.render();
}-*/;
}
As noted by the inline comments, use of javascript array fails. Failure is not because js array usage, such as [0,0,0] or new Array(0,0,0) is wrong. Failure is because the way XTK code checks for "instance of Array".
Edit: 2
Dear XTK
I was able to checkout XTK code from git, make changes that I'm proposing, re-build XTK.js and finally test successfully that my fix solves the problem.
for example: in displayable.js I commented one line and added another line thus:
// if (!goog.isDefAndNotNull(color) || !(color instanceof Array) || (color.length != 3)) {
if (!goog.isDefAndNotNull(color) || !(goog.isArray(color)) || (color.length != 3)) {
I made similar changes in couple of other places in the xtk codebase to get my usecase going. Explanation of why this is the right solution is here: Closure: The Definitive Guide. Would you please consider making this fix in the codebase for release 8? Thank you

Using XTK with GWT ? What do you mean ? Did you write your own wrappers to compile code with xtk calls from Java to JavaScript ? Or do you directly use xtk.js in the war file and write manualy some JavaScript using it ? Or do you only use GAE (Google App Engine), the Google environnement for web applications (the ones made with GWT, but also not compiled from Java ones). Could you be more accurate please ?
Here they deal with some issues with GWT and type test, did you try to create your array with the "new" operator ?
var mycolor = new Array(0.7,0,0);
mesh.color = mycolor;

Related

How to use Windows.UI.ViewManagement.UIViewSettings from WinRT/C++?

We have a pure Win32/C++ app from which we want to be able to detect Tablet Mode on Windows 10.
I have the following code which came from somewhere which uses WRL to access the Windows.UI.ViewManagement.UIViewSettings.UserInteractionMode property:
WRL::ComPtr<IUIViewSettingsInterop> interop;
if (SUCCEEDED(Windows::Foundation::GetActivationFactory(WRL::Wrappers::HStringReference(
RuntimeClass_Windows_UI_ViewManagement_UIViewSettings).Get(),
&interop)) && interop)
{
WRL::ComPtr<vm::IUIViewSettings> pViewSettings;
if (SUCCEEDED(interop->GetForWindow(hWnd, IID_PPV_ARGS(&pViewSettings))) && pViewSettings)
{
vm::UserInteractionMode currentMode;
if (SUCCEEDED(pViewSettings->get_UserInteractionMode(&currentMode)))
return currentMode == vm::UserInteractionMode::UserInteractionMode_Touch;
}
}
This works fine, however we also have another function using WinRT and I gather WinRT is the current technology we should be using for this, so I was trying to work out how to convert the WRL code.
I came up with this code, which compiles fine, but throws an exception in GetForCurrentView():
auto uiSettings = winrt::Windows::UI::ViewManagement::UIViewSettings::GetForCurrentView();
return uiSettings.UserInteractionMode() == winrt::Windows::UI::ViewManagement::UserInteractionMode::Touch;
The error is HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND). I'm assuming there's something I'm meant to be doing to initialise the "current view", similar to how the WRL code provides a window handle to GetForWindow, but I haven't been able to work out how or what that is.
Thanks to #RaymondChen the C++/WinRT equivalent of the WRL code in my question is:
auto uiSettings = winrt::capture<winrt::Windows::UI::ViewManagement::UIViewSettings>
(winrt::get_activation_factory<winrt::Windows::UI::ViewManagement::UIViewSettings>()
.as<IUIViewSettingsInterop>(), &IUIViewSettingsInterop::GetForWindow, hWnd);
return uiSettings.UserInteractionMode() == winrt::Windows::UI::ViewManagement::UserInteractionMode::Touch;

Can I get a Flutter surface texture with Flutter FFI?

This is part of the code for a Flutter plugin that uses a Flutter texture to render something with OpenGL
private FlutterVideoPlugin(Registrar registrar, MethodChannel channel) {
this.registrar = registrar;
this.channel = channel;
this.textures = registrar.textures();
}
#Override
public void onMethodCall(MethodCall call, Result notSafeResult) {
final AnyThreadResult result = new AnyThreadResult(notSafeResult);
if (call.method.equals("createVideoRenderer")) {
TextureRegistry.SurfaceTextureEntry entry = textures.createSurfaceTexture();
//do anything with the texture
As you see, it uses a Flutter methodCall.
Is it possible to do the same thing but with FFI in C++? I'd not like to rely on onMethodCall, it's gotta be either FFI or Flutter method calls, because I think that FFI does everything in a different way, something like that.
However, since the Flutter engine and specially the Texture part is written in Java, it looks like I need some java code. The problem is that FFI and java don't mix. It looks like I either have to use onMethodCall for everything in my app, or FFI, but FFI does not seem to have support for getting textures
There is an open issue: Consider exposing plugin APIs for use via FFI
#110353
Discussion on the issue refers specifically to the Texture Registry so I believe the answer is no (for now).

VSIX how to get current snapshot document name?

I have been trying to to create an extension that highlights specific line numbers for me in Visual Studio in the margins.
I manged to get my marking in the margins using predefined line number but for it to work properly I need to know what the current document FullName is (Path and filename)
After much googling I figured out how to do it with the sample code (which is not ideal)
DTE2 dte = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.15.0");
var activeDocument = dte.ActiveDocument;
var docName = activeDocument.Name;
var docFullName = activeDocument.FullName;
Now I know the problems here
is that is for specific version bases on the text
there is no way to select which instance (when running more than one VS)
It seems to be very slow
I have a feeling I should be doing this with MEF Attributes but the MS docs examples are so simple that they do not work for me. I scanned a few SO questions too and I just cannot get them to work. They mostly talk about Services.. which I do not have and have no idea how to get.
The rest of my code uses SnapshotSpans as in the example Extension of Todo_Classification examples which is great if you do NOT need to know the file name.
I have never done any extensions development. Please can somebody help me do this correctly.
You can use following code to get a file from a snapshot without any dependencies.
public string GetDocumentPath(Microsoft.VisualStudio.Text.ITextSnapshot ts)
{
Microsoft.VisualStudio.Text.ITextDocument textDoc;
bool rc = ts.TextBuffer.Properties.TryGetProperty(
typeof(Microsoft.VisualStudio.Text.ITextDocument), out textDoc);
if (rc && textDoc != null)
return textDoc.FilePath;
return null;
}
If you don't mind adding Microsoft.CodeAnalysis.EditorFeatures.Text to your project it will provide you with an extension method Document GetOpenDocumentInCurrentContextWithChanges() on the Microsoft.VisualStudio.Text.Snapshot class. (Plus many other Rosyln based helpers)
using Microsoft.CodeAnalysis.Text;
Document doc = span.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

Java El and persistent XSS

First of all this is just a POC that seems to be working fine so far, basically I would like to know what you think of this kind of solution.
The problem:
I have this app with lot of EL code and now I'm supposed to prevent XSS errors my current solution so far (does not mean its the only thing I'm going to fix though) only involves preventing XSS in an EL context.
code below is in a jsp page:
This is your car color ${car.color}
car object is set as follows:
ModelAndView mv = new ModelAndView("page3");
String xss = "<script>window.alert('XSS!!');</script>";
Car c = new Car(xss);
mv.addObject("car", c);
return mv;
this way an alert message should popup, nothing new here...
my proposal (which is working ) is as follows
This is your car color${car.color.preventXSS}
this means like "adding" a preventXSS property to every String (I'm not modifying String class), I'm achieving this through a custom EL Resolver which is basically as follows
try {
value = context.getELResolver().getValue(context, base, property);
} catch (PropertyNotFoundException e) {
if ((base instanceof String) && property.equals(PREVENT_XSS)) {
context.setPropertyResolved(true);
value = ESAPI.encoder().encodeForHTML(base.toString());
}
}
where 'base' is in this case has the value <script>window.alert('XSS!!');<;/script>
and 'PREVENT_XSS' is the constant for 'preventXSS' which is the last property in our EL expression.
BTW I'm using OWASP esapi encoder for dealing with XSS, if needed this can be improved by adding other different encodings like encodeForHTMLAttribute and many other that OWASP's esapi supports.
note1: I know I can use an EL function to achieve the same but I wanted to just add 'property' preventXSS without having to include a jsp taglib directive in every page.
note2: I also know about the c:out jstl tag.
Yeah, the solution that you are proposing works, however one issue that I see is that the additional property can be confusing at maintenance time, due to the fact that is not following the EL standards. If you are using Spring MVC, you can take a look of the following approaches to prevent XSS:
Referece 1
Referece 2
Referece 3

C++\IronPython integration example code?

I'm looking for a simple example code for C++\IronPython integration, i.e. embedding python code inside a C++, or better yet, Visual C++ program.
The example code should include: how to share objects between the languages, how to call functions\methods back and forth etc...
Also, an explicit setup procedure would help too. (How to include the Python runtime dll in Visual Studio etc...)
I've found a nice example for C#\IronPython here, but couldn't find C++\IronPython example code.
UPDATE - I've written a more generic example (plus a link to a zip file containing the entire VS2008 project) as entry on my blog here.
Sorry, I am so late to the game, but here is how I have integrated IronPython into a C++/cli app in Visual Studio 2008 - .net 3.5. (actually mixed mode app with C/C++)
I write add-ons for a map making applicaiton written in Assembly. The API is exposed so that C/C++ add-ons can be written. I mix C/C++ with C++/cli. Some of the elements from this example are from the API (such as XPCALL and CmdEnd() - please just ignore them)
///////////////////////////////////////////////////////////////////////
void XPCALL PythonCmd2(int Result, int Result1, int Result2)
{
if(Result==X_OK)
{
try
{
String^ filename = gcnew String(txtFileName);
String^ path = Assembly::GetExecutingAssembly()->Location;
ScriptEngine^ engine = Python::CreateEngine();
ScriptScope^ scope = engine->CreateScope();
ScriptSource^ source = engine->CreateScriptSourceFromFile(String::Concat(Path::GetDirectoryName(path), "\\scripts\\", filename + ".py"));
scope->SetVariable("DrawingList", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingList::typeid));
scope->SetVariable("DrawingElement", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingElement::typeid));
scope->SetVariable("DrawingPath", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingPath::typeid));
scope->SetVariable("Node", DynamicHelpers::GetPythonTypeFromType(AddIn::Node::typeid));
source->Execute(scope);
}
catch(Exception ^e)
{
Console::WriteLine(e->ToString());
CmdEnd();
}
}
else
{
CmdEnd();
}
}
///////////////////////////////////////////////////////////////////////////////
As you can see, I expose to IronPython some objects (DrawingList, DrawingElement, DrawingPath & Node). These objects are C++/cli objects that I created to expose "things" to IronPython.
When the C++/cli source->Execute(scope) line is called, the only python line
to run is the DrawingList.RequestData.
RequestData takes a delegate and a data type.
When the C++/cli code is done, it calls the delegate pointing to the
function "diamond"
In the function diamond it retrieves the requested data with the call to
DrawingList.RequestedValue() The call to DrawingList.AddElement(dp) adds the
new element to the Applications visual Database.
And lastly the call to DrawingList.EndCommand() tells the FastCAD engine to
clean up and end the running of the plugin.
import clr
def diamond(Result1, Result2, Result3):
if(Result1 == 0):
dp = DrawingPath()
dp.drawingStuff.EntityColor = 2
dp.drawingStuff.SecondEntityColor = 2
n = DrawingList.RequestedValue()
dp.Nodes.Add(Node(n.X-50,n.Y+25))
dp.Nodes.Add(Node(n.X-25,n.Y+50))
dp.Nodes.Add(Node(n.X+25,n.Y+50))
dp.Nodes.Add(Node(n.X+50,n.Y+25))
dp.Nodes.Add(Node(n.X,n.Y-40))
DrawingList.AddElement(dp)
DrawingList.EndCommand()
DrawingList.RequestData(diamond, DrawingList.RequestType.PointType)
I hope this is what you were looking for.
If you don't need .NET functionality, you could rely on embedding Python instead of IronPython. See Python's documentation on Embedding Python in Another Application for more info and an example. If you don't mind being dependent on BOOST, you could try out its Python integration library.