Efficient way of updating colors of model3d elements - sharpdx

I'm using a Viewport3DX with a lot of different MeshGeometryModel3D elements.
The User Interface integrates a slider that will update the opacity (alpha-value of PhongMaterials) of all model3d elements.
This is my current implementation of the code that updates the opacity:
geometryhandler.cs
public void UpdateOpacity(double value)
{
if (_mainWindow.MyBuildingComponents == null) return;
foreach (var component in _mainWindow.MyBuildingComponents)
{
// assign new material and later assign it back, to get the changes of the material recognized
var newmaterial = (_meshIdTogeometryModel3D[component.Id].Material as PhongMaterial).Clone();
// create new DiffusColor because setting the alpha property directly is not possible
newmaterial.DiffuseColor = new Color4(newmaterial.DiffuseColor.Red, newmaterial.DiffuseColor.Green, newmaterial.DiffuseColor.Blue, (float)value);
_meshIdTogeometryModel3D[component.Id].Material = newmaterial;
}
}
MainWindow.xaml.cs
private void UpdateOpacity(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Geometryhandler?.UpdateOpacity(SliderModelOpacity.Value);
}
The UpdateOpacity function is called each time the value of the slider changes, iterates through a dictionary of MeshGeometryModel3D elements and updates their materials.
I tried many different version, but in the end this was the only implementation that did the job. However the update is very slow and 'laggy', even in release mode.
I recognized two things:
I had to clone the existing material, update it and assign it back to get the material to change in the viewport
I couldn't directly set the alpha-property of the Diffusecolor, but instead instantiate a new color object
Does somebody have an idea where the bottleneck might be here. Is it the cloning of the material, instantiating the new color or both? Or something completely different? Are there any better ways of doing the updates?
Curious to hear your suggestion. Thanks a lot already!

I'm going to reference my comment here so we can close out this question.
Are you using the "ValueChanged" event to trigger the UpdateOpacity? You might want to look into only updating the Opacity when the user is done dragging the slider: social.msdn.microsoft.com/Forums/vstudio/en-US/…. The only other suggestion I have is to try and combine/group together elements that have the same base color, so there are fewer material changes required with the opacity update.
...
msdn.microsoft.com/en-us/library/bb613553.aspx

Related

Highcharts - SetState (inactive) not working after redraw

Consider the following toy example: jsfiddle
chart: {
events: {
render: myfunc
}
},
...
function myfunc() {
var chart = this;
chart.series.forEach(function(s) {
s.setState('inactive', true);
});
chart.series[0].setState('hover');
}
The intended behavior is to set the state of the first series as hover while setting all other series as inactive after load and redraw events.
The code works as intended after load
However, it doesn't work after redraw (via the select box). After selecting an option in the box, the series are rendered as normal instead of as inactive UNLESS any series had been previously hovered. That is, the code works if you interact with any series AND THEN select an option in the box, but it doesn't work if you select an option in the box immediately after the loading without interacting on the series.
Surprisingly, after some inspection in the console, I noted that in any case the intended states are properly passed to the series object. So, why the states are not properly rendered?
*NOTE: In my actual application the hovered series is not necessarily the first one, but it varies depending on the output of other function. I'm aware that "myfunc" could be simplified in the current example, but for general purposes please suggest an answer keeping the basic structure if possible.
This seems to be related to this issue from HighChart's GitHub. In your case, HighCharts is correctly updating the series' state. However, while rendering it fails to set the proper opacity value associated with the 'inactive' state. The workaround is to explicitly set the opacity of the series to the same value it should have in the 'inactive' state.
// F U N C T I O N S E T S T A T E
function myfunc() {
var chart = this;
chart.series.forEach(function(s) {
//explicit opacity
s.opacity = 0.2;
s.setState('inactive', true);
});
chart.series[0].setState('hover');
}
Thank you for sharing this issue, it seems that it is a regression after the last release.
In the previous version it used to work fine: https://jsfiddle.net/BlackLabel/Lbpvdwam/
<script src="https://code.highcharts.com/8.1.0/highcharts.js"></script>
<script src="https://code.highcharts.com/8.1.0/highcharts-more.js"></script>
I reported it on Highcharts Github issue channel where you can follow this thread: https://github.com/highcharts/highcharts/issues/13719
If you don't need any new features/bugfixes use the previous version as a temporary workaround.

How to set start time for primefaces clock widget?

Is it possible to set start time for PrimeFaces clock widget?
I know about Analog Clock from PrimeFaces Extensions, but I need digital clock with this option.
I have tried to override javascript method for this clock, but it doesn't work.
PrimeFaces.widget.Clock = PrimeFaces.widget.BaseWidget.extend({
init: function(cfg) {
this._super(cfg);
this.cfg.value = 1430304817141;
this.current = this.isClient() ? new Date() : new Date(this.cfg.value);
var $this = this;
},
isClient: function() {
return this.cfg.mode === 'client';
},
});
The first problem was that you were overriding the widget too late, after PrimeFaces has instantiated its original unmodified widget. According to PrimeFaces Customizable Resource Ordering the right place to override would be in h:head.
The second problem is that you're overriding the widget with a crippled version, that doesn't contain many necessary functions that were present in the original widget.
I wouldn't recommend this approach at all - to basically break the whole PrimeFaces widget like that. What if you'd want to use the normal unchanged clock? Copy-pasted code is harder to maintain too. I advise going the more localized approach: tweak only a single widget instance.
<p:clock id="my_clock" />
<script>
// get widgetVar via EL function, since p:clock doesn't have the widgetVar attribute
var myClockVar = #{p:widgetVar('my_clock')};
myClockVar.current = new Date(1430304817141);
</script>
Just be careful not to update the clock with AJAX requests, or it will to reset to showing client time; and not to update the script, or the clock will reset to the specified time again.

Getting selected ListBox values on button Click | ZK

I am very new to ZK framework and trying to customize few things and have struck at one point which I am not sure how to achieve that.
I have a predefined section where I need to show 2 drop down and a button and need to persist those drop down values on button click event.
This is how It has been define in Spring file
<bean id="mybean" parent="parentBean" class="WidgetRenderer">
<property name="detailRenderer">
<bean class="DetailsListRenderer" parent="abstractWidgetDetailRenderer"/>
</property>
</bean>
Here mybean is being used to show main section and I am adding my drop down using this bean while button are being added to detailRenderer.
Save button is bind to onClick event, but I am not sure how I can fetch values from my custom drop down?
I am aware about binding those Dropdown with onClick event but they have to be in same class.
Can any one suggest me how I can fetch values of those drop down.I am creating down down with following code
Listbox listbox = new Listbox();
listbox.appendItem("item1", "item1");
listbox.appendItem("item2", "item2");
This is my button code in another class
protected void createUpdateStatusButton(Widget widget,Div container)
{
Button button = new Button(LabelUtils.getLabel(widget, buttonLabelName, new Object[0]));
button.setParent(container);
button.addEventListener("onClick", new EventListener()
{
public void onEvent(Event event)throws Exception
{
MyClass.this.handleSaveStatusEvent(widget, event);
}
});
}
You may want to listen to the onSelect (I prefer to use Events.ON_SELECT rather than writing the strings) which is more specific to when the Listbox selection changes.
Either way, the key is to get the information you want from the Event passed to your EventListener, rather than going back to your Listbox itself. The basic Event usually carries useful information on getTarget and getData but using more specific events (SelectEvent in this case) will give you access to more relevant info.
button.addEventListener(Events.ON_SELECT, new EventListener<SelectEvent<Listitem, MyDataObject>() {
public void onEvent(SelectEvent<Listitem, MyDataObject> event) {
// Now you can access the details of the selection event..
List<Listitem> selectedItems = event.getSelectedItems();
List<MyDataObject> selectedObjects = event.getSelectedObjects();
}
});
You can find out what events are available for different ZK widgets in their Component Reference documentation.
If I understand the question (I don't think I did in my previous response) you want to gather information from the page (eg: Listbox selection state) when the user clicks a button. Your problem being that you are using disparate classes to compose the page and so don't have access to the various ZK Components when the button is clicked.
(Ignoring the multiple class issue for a minute)
From a high level, there are sort of two camps in the ZK community. The newer MVVM approach suggests the view should push the relevant state to the back end as the user interacts with the front end. This way, the back end never needs to ask for the client state, and when the button is clicked, the values/state are on the server ready to be used.
The other camp binds the client to the server such that the back end always has access to the client Components and when the button is clicked, the values/state can easily be retrieved by interacting with the components.
Another approach is more like what I was talking about in my previous answer, to not bind the back end to the client at all but to rely on event data as much as possible. I favor this approach where it is sufficient.
Now, you're free to choose your favored approach and ZK has lots of documentation on how to work in either of these camps. The question then is where is the client state stored on the server (either pushed there by the client in MVVM or bound there in MVC). I don't think that's a question that can be solved here, that's a software engineering challenge. I personally suggest you take on standard ZK patterns so as not to but heads with the framework. If you really want to go your route, you can grab a reference to the Listbox on the fly like so:
public class Foo {
public static final String LISTBOX_ID = "myListbox";
public void renderListbox(Component parent, MyItem items) {
Listbox listbox = new Listbox();
listbox.setId(LISTBOX_ID);
listbox.setParent(parent);
for (MyItem item : items) {
listbox.appendItem(item.getName(), item);
}
}
}
public class Bar {
#Listen(Events.ON_CLICK + " = #saveButton")
public void saveButtonClicked(Event event) {
Component saveButton = event.getTarget();
Listbox listbox = (Listbox) saveButton.getFellow(Foo.LISTBOX_ID);
Set<Listitem> selection = listbox.getSelectedItems();
// do something
}

When I add a stream subscription on a DOM element, and then remove the element later, will dart:html take care of the subscription, too?

Often, I create an element from which I need to listen to (Mouse)Events. Then, later, I don't need the button anymore, so I remove it. Like this:
var button = new ButtonElement()
..onClick.listen((_) => print("Clicked!"));
// ... somewhere else in the code or in the callback above
button.remove();
Should I also take care of the onClick subscription (to prevent memory leaks) or is that handled for me?
I suspect this will not be done automatically by removing the element from the DOM, as you might re-insert it at another point. The source code for remove() is fairly simple:
Node remove() {
// TODO(jmesserly): is parent == null an error?
if (parentNode != null) {
parentNode.nodes.remove(this);
}
return this;
}
However, if there are no other references to the object and the Element class gets cleaned up by the garbage collector, then I expect the subscriptions will go with it (though I don't know enough to know how to confirm this from the source!).

Dart web-ui not updated when data received from network

I have the following fragment in a web component:
<div id="mycodes">
<template iterate='code in codeList'>
{{code}}
</template>
</div>
And in a Dart file, codeList is populated when the user clicks on a button:
void onMyButtonClick(Event event) {
HttpRequest.getString('http://getData').then((response) {
mylist = json.parse(response);
for(var code in mylist){
codeList.add(code['c']);
}
}
The problem is that I don't see data on first click. I need to click the button twice to see data.
But if I fill codeList manually (not from network data) as shown below, then I see the data on first click:
void onMyButtonClick(Event event) {
codeList.add("data 1");
codeList.add("data 2");
}
}
I need the template to iterate after the network data is available. It appears that event loop has already done its job of painting a page before the network data becomes available through future object.
Is there a way to refresh the page after model is updated in dart?
The reason your codeList currently populates if you add it with the on-click event is because the current web_ui has 'watchers' which automatically are called when an event happens. You then populate the list synchronously. However one of the downfalls of watchers is exactly your use case, when the data is updated asynchronously then the watchers don't reflect changes in time.
As a result the watchers are being phased out and replaced with observables. Observables allow us to flag a variable to be watched for reassignment and when that happens it will cause the view to change. For example:
#observable int x = 0;
// ...
x = 1;
When the x = 1 is called later in the code it automatically triggers the views to update. This leaves us with one problem however. When you are adding to a list, you are not reassigning the value itself. As such, observables also offer a function to convert a list to an observable list (this also works for maps).
For instance if you changed your declaration of codeList to something like the following, then when you add to the list later it will update accordingly.
var codeList = toObservable([]); // Assuming it starts with an empty list
// or
var codeList = toObservable(_startCodeList); // if you already have a list
Also see the Dart Tutorial: Target 7 for more information on using #observable and toObservable.
For more in-depth information, check out the article on Observables and Data Binding
You need to mark the fields you want WebUi to monitor with the #observable annotation. Otherwise you only get the initial value not any subsequent updates.
You can do this either directly on the object declaration or you can make the entire class as observable and all its fields will then be observed.
For an example see http://www.dartlang.org/docs/tutorials/custom-elements/#using-two-way-data-binding

Resources